xml XmlWriter 写入带有属性的元素字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9025288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:08:07  来源:igfitidea点击:

XmlWriter to write Element String with Attribute

xml

提问by Sanjay

I am using XmlWriter and I am wondering if some one ever tried to write the xml element string (leaf node) with attributes so that the output would look like

我正在使用 XmlWriter,我想知道是否有人尝试用属性编写 xml 元素字符串(叶节点),以便输出看起来像

<book id='1' author='j.k.rowling' year='2010'>999</book>

instead of

代替

<book id='1' author='j.k.rowling' year='2010'> 
    <book>999</book>
</book>

回答by Raj Ranjhan

You can use WriteString...

您可以使用WriteString...

using (XmlWriter writer = XmlWriter.Create("books.xml"))
{

    writer.WriteStartElement("book"); 
    writer.WriteAttributeString("author", "j.k.rowling"); 
    writer.WriteAttributeString("year", "1990");
    writer.WriteString("99");
    writer.WriteEndElement();                                

}