编写具有属性的 xmlns 元素 [英] Write xmlns element with attributes
本文介绍了编写具有属性的 xmlns 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 XmlWriter 生成 XML 文件.我正在尝试复制一个旧的 XML 文件,我想创建一个看起来像的条目;
I'm using XmlWriter to generate an XML file. I am trying to replicate an old XML file and I want to create an entry that will look like;
<Return xmlns="http://address/here" appName="Data Return - Collection Tool" appVer="1.1.0">
我的代码如下:
writer.WriteStartElement("Return", "http://address/here")
writer.WriteAttributeString("appName", "Data Return - Collection Tool")
writer.WriteAttributeString("appVer", "1.1.0")
这是以错误的顺序生成属性,即.
This is generating the attributes in the wrong order ie.
<Return appName="Data Return - Collection Tool" appVer="1.1.0" xmlns="http://address/here">
我怎样才能让这些按照我想要的顺序出现.请提供任何帮助.
How can i get these to appear in the order i want. Any help please.
推荐答案
XmlWriter
允许您在需要时编写 xmlns 属性,前提是该值与 WriteStartElement 中指定的值相同代码>:
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());
}
输出:
<Return xmlns="http://address/here" appName="Data Return - Collection Tool" appVer="1.1.0" />
这篇关于编写具有属性的 xmlns 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文