Best .net Method to create an XML Doc

前端 未结 11 680
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 03:33

I am trying to figure out what the best method is for writing an XML Document. Below is a simple example of what I am trying to create off of data I am pulling from our ERP syst

11条回答
  •  时光说笑
    2021-02-04 04:09

    If your use case is simple, nothing could possibly simpler and easier to use than XmlTextWriter. That said, one alternative would be to use an XmlDocument object to create and append all of your nodes. But I think it's easier to write and maintain code that uses XmlTextWriter if you're creating a document from scratch rather than manipulating one. Using XmlTextWriter should be very straightforward:

            StringBuilder output = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(output);
            writer.WriteProcessingInstruction("xml", "version=\"1.0\"");
            writer.WriteStartElement("Orders");
            //...start loop...
            writer.WriteStartElement("Order");
            writer.WriteAttributeString("OrderNumber", "12345");
            writer.WriteElementString("ItemNumber", "0123993587");
            writer.WriteElementString("QTY", "10");
            writer.WriteElementString("WareHouse", "PA019");
            writer.WriteEndElement();
            //...loop...
            writer.WriteEndElement();
            writer.Close();
    

提交回复
热议问题