如何在.NET中使用XmlWriter创建XmlDocument?

许多.NET函数使用XmlWriter来输出/生成xml。 输出到文件/字符串/内存是非常简单的操作:

XmlWriter xw = XmlWriter.Create(PutYourStreamFileWriterEtcHere);
xw.WriteStartElement("root");
...

有时,您需要操纵生成的Xml,因此希望将其加载到XmlDocument中,或者由于其他原因可能需要XmlDocument,但是必须使用XmlWriter生成XML。 例如,如果您在第三方库中调用仅输出到XmlWriter的函数。

您可以做的一件事是将xml写入字符串,然后将其加载到XmlDocument中:

StringWriter S = new StringWriter();
XmlWriter xw = XmlWriter.Create(S);
/* write away */
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(S.ToString());

但这效率很低-首先将所有xml信息序列化为一个字符串,然后再次解析该字符串以创建DOM。

您如何指向XmlWriter直接构建XmlDocument?

Boaz asked 2020-02-22T02:36:00Z
5个解决方案
82 votes

这是至少一种解决方案:

XmlDocument doc = new XmlDocument(); 
using (XmlWriter writer = doc.CreateNavigator().AppendChild()) 
{ 
    // Do this directly 
     writer.WriteStartDocument(); 
     writer.WriteStartElement("root"); 
     writer.WriteElementString("foo", "bar"); 
     writer.WriteEndElement(); 
     writer.WriteEndDocument();
    // or anything else you want to with writer, like calling functions etc.
}

显然,当您调用AppendChild()时,XpathNavigator给您一个XmlWriter。

Cぢごとととまhごごごごごごごとと:ごご:と/ / /。 t。

Boaz answered 2020-02-22T02:36:25Z
9 votes

您可以做相反的事情:首先使用DOM构建XmlDocument,然后将其写入XmlWriter

XmlDocument xdoc = new XmlDocument();
... // build the document

StringWriter S = new StringWriter();
XmlWriter xw = XmlWriter.Create(S);
xdoc.WriteTo(xw);
Thomas Levesque answered 2020-02-22T02:36:45Z
7 votes

您可以使用XMLWriter类编写xml文件。这是一个例子。

    XmlWriterSettings objSetting = new XmlWriterSettings();
    objSetting.Indent = true;
    objSetting.NewLineOnAttributes = true;

    System.Text.StringBuilder sb = new System.Text.StringBuilder();


    using (XmlWriter objWriter = XmlWriter.Create(sb, objSetting))
    {
        //Note the artificial, but useful, indenting
        objWriter.WriteStartDocument();

        objWriter.WriteStartElement("books");

        ////////Start Book Element///////

        objWriter.WriteStartElement("book");

        objWriter.WriteStartAttribute("ISBN");
        objWriter.WriteValue("asp1");
        objWriter.WriteEndAttribute();

        objWriter.WriteStartElement("Title");
        objWriter.WriteValue("ASP.NET");
        objWriter.WriteEndElement();

        objWriter.WriteElementString("ReleaseDate", "11/11/2010");

        objWriter.WriteStartElement("Pages");
        objWriter.WriteValue(200);
        objWriter.WriteEndElement(); //price

        objWriter.WriteEndElement(); //book
        ////////End Book Element///////


        ////////Another Element

        ////////Start Book Element///////

        objWriter.WriteStartElement("book");

        objWriter.WriteStartAttribute("ISBN");
        objWriter.WriteValue("c#2");
        objWriter.WriteEndAttribute();

        objWriter.WriteStartElement("Title");
        objWriter.WriteValue("C#.NET");
        objWriter.WriteEndElement();

        objWriter.WriteElementString("ReleaseDate", "10/11/2010");

        objWriter.WriteStartElement("Pages");
        objWriter.WriteValue(500);
        objWriter.WriteEndElement(); 

        objWriter.WriteEndElement(); //book
        ////////End Book Element///////



        objWriter.WriteEndElement(); //books
        objWriter.WriteEndDocument();

    }

    File.WriteAllText(Server.MapPath("BooksList.xml"), sb.ToString());
Jayesh Sorathia answered 2020-02-22T02:37:05Z
4 votes

XmlWriter背后的想法是等到您完成对数据的修改之后再开始编写。

XmlWriter并不是在考虑您的情况下构建的。

要么

  • 等到知道数据是什么在写作之前会

要么

  • 做你目前正在做的事情
Binary Worrier answered 2020-02-22T02:37:47Z
1 votes

XmlWriter写入了一个基础的Stream对象,如果它是双向的(MemoryStream),则可以简单地将其重新定位为-0-,然后在XmlDocument.Load(stream)中使用Stream对象。

HTH,

Zach Bonham answered 2020-02-22T02:38:16Z
translate from https://stackoverflow.com:/questions/1346995/how-to-create-a-xmldocument-using-xmlwriter-in-net