Best .net Method to create an XML Doc

前端 未结 11 977
爱一瞬间的悲伤
爱一瞬间的悲伤 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 03:46

    I found it largely depends how complex your original data is.

    If your data is nicely organized in objects and it is sufficient to dump it in XML, Linq is very verbose and powerful. But as soon as there are object inter-dependencies, I don't think you want to go with the Linq one-liner, as this is real pain to debug and/or extend.

    For those cases, I'd prefer to go with XmlDocument, create a help method to facilitate adding attributes to an element (see below), and use Linq in foreach loops around the XML creation blocks.

    private void XAttr(ref XmlNode xn, string nodeName, string nodeValue)
    {
        XmlAttribute result = xn.OwnerDocument.CreateAttribute(nodeName); 
        result.InnerText = nodeValue;
        xn.Attributes.Append(result);
    }
    

提交回复
热议问题