Write xmlns element with attributes

陌路散爱 提交于 2019-12-02 01:11:58

XmlWriter allow you to write the xmlns attribute when you want if the value is the same than the one specified in WriteStartElement :

void Main()
{
    StringWriter stringWriter = new StringWriter();
    using(XmlWriter writer = XmlWriter.Create(stringWriter))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("Return", "http://address/here");
        writer.WriteAttributeString("xmlns", "http://address/here");
        writer.WriteAttributeString("appName", "Data Return - Collection Tool");
        writer.WriteAttributeString("appVer", "1.1.0");
        writer.WriteEndElement();
        writer.WriteEndDocument();
    }

    Console.WriteLine(stringWriter.ToString());
}

Output :

<Return xmlns="http://address/here" appName="Data Return - Collection Tool" appVer="1.1.0" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!