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

发布于 2024-08-02 18:20:11 字数 659 浏览 12 评论 0原文

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

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

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

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

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.

One of the things you can do is write the xml to a string and then load it into your 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

非要怀念 2024-08-09 18:20:12

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

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

要么

  • 等到你知道你的数据是什么
    将在写作

  • 做你目前正在做的事情之前

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

XmlWriter wasn't built with your situation in mind.

Either

  • Wait until you know what your data is
    going to be before writing

or

  • Do what you're currently doing
貪欢 2024-08-09 18:20:12

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

HTH,

Z

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

HTH,

Z

后来的我们 2024-08-09 18:20:11

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

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

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

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()

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

无言温柔 2024-08-09 18:20:11

您可以执行相反的操作:首先使用 DOM 构建 XmlDocument ,然后将其写入 XmlWriter

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

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

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

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

StringWriter S = new StringWriter();
XmlWriter xw = XmlWriter.Create(S);
xdoc.WriteTo(xw);
失去的东西太少 2024-08-09 18:20:11

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

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

    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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文