How to create a XmlDocument using XmlWriter in .NET?

前端 未结 5 1288
傲寒
傲寒 2020-12-08 02:00

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

XmlWriter xw = XmlWriter.Create(PutYourStre         


        
5条回答
  •  感动是毒
    2020-12-08 02:28

    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

提交回复
热议问题