XmlWriter用Attribute编写Element String

时间:2012-01-26 21:01:49

标签: xml

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

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

而不是

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

1 个答案:

答案 0 :(得分:48)

您可以使用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();                                

}