C# XmlDocument Nodes

有些话、适合烂在心里 提交于 2019-12-01 22:34:12

An XML document can only ever have one root node. Otherwise it's not well formed. You will need to create 2 xml documents and join them together if you need to send both at once.

Its throwing an exception because you are trying to create invalid xml. XmlDocument will only generate well formed xml.

You could do it using an XMLWriter and setting XmlWriterSettings.ConformanceLevel to Fragment or you could create two XmlDocuments and write them out into the same stream.

Build two separate XML documents and concatenate their string representation.

It looks like your node structure always be the same. (I don't see any conditional logic.) If the structure is constant you could define an XML template string. Load that string into an XML Document & do a SelectNode to populate individual nodes.

That may be simpler/cleaner than programatically creating the root, elements & nodes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!