How to create a XmlDocument using XmlWriter in .NET?

前端 未结 5 1258
傲寒
傲寒 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:27

    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);
    
    0 讨论(0)
  • 2020-12-08 02:27

    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());
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-08 02:31

    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
    0 讨论(0)
  • 2020-12-08 02:34

    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

    0 讨论(0)
提交回复
热议问题