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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1346995/
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-03 13:13:38  来源:igfitidea点击:

How to create a XmlDocument using XmlWriter in .NET?

.netxmldocumentxmlwriter

提问by Boaz

Many .NET functions use XmlWriter to output/generate xml. Outputting to a file/string/memory is a very operation:

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

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

Sometimes , you need to manipulate the resulting Xml and would therefore like to load it into a XmlDocument or might need an XmlDocument for some other reason but you must generate the XML using an XmlWriter. For example, if you call a function in a 3rd party library that outputs to a XmlWriter only.

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

One of the things you can do is write the xml to a string and then load it into your XmlDocument:

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

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

However this is inefficient - first you serialize all the xml info into a string, then you parse the string again to create the DOM.

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

How can you point an XmlWriter to build a XmlDocument directly?

如何指向 XmlWriter 直接构建 XmlDocument?

回答by Boaz

Here's at least one solution:

这里至少有一个解决方案:

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.
}

Apparently XpathNavigator gives you a XmlWriter when you call AppendChild()

显然,当您调用 AppendChild() 时,XpathNavigator 会为您提供一个 XmlWriter

Credits go to Martin Honnen on : http://groups.google.com/group/microsoft.public.dotnet.xml/browse_thread/thread/24e4c8d249ad8299?pli=1

学分转到 Martin Honnen 上:http://groups.google.com/group/microsoft.public.dotnet.xml/browse_thread/thread/24e4c8d249ad8299?pli=1

回答by Thomas Levesque

You could do the opposite : build the XmlDocumentfirst using DOM, then write it to a XmlWriter:

你可以做相反的事情:XmlDocument使用 DOM构建第一个,然后将其写入XmlWriter

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

StringWriter S = new StringWriter();
XmlWriter xw = XmlWriter.Create(S);
xdoc.WriteTo(xw);

回答by Jayesh Sorathia

You can write xml file using XMLWriter class. Here is example for this.

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

回答by Binary Worrier

The idea behind XmlWriter is to wait until you have finished modifying your data before you start writing.

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

XmlWriter wasn't built with your situation in mind.

XmlWriter 的构建并没有考虑到您的情况。

Either

任何一个

  • Wait until you know what your data is going to be before writing
  • 等到你知道你的数据会是什么再写

or

或者

  • Do what you're currently doing
  • 做你目前正在做的事情

回答by Zach Bonham

There is an underlying Stream object that the XmlWriter was writing to, if it was bidirectional (MemoryStream), you could simply re-position it back to -0- and then use the Stream object in the XmlDocument.Load(stream).

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

HTH,

哈,

Z

Z