且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得子节点&使用C#动态生成父节点的子节点的值

更新时间:2023-01-26 09:01:49

如果您不熟悉Linq< => XML(System.Xml.Linq):[
如果要向节点添加新的子节点:首先使用.Element获取节点:然后在该节点上使用.Add方法:
If you are not familiar with Linq<=>XML (System.Xml.Linq):[^].

If you are already using Linq<=>XML, you might review the use of XElement:[^].

In any case you want your tree-structured information loaded into an XDocument. You can access all the top level nodes of your XDocument using XDocument.Descendants.

You find an Element by using .Element("elementName") method on the parent Node of the Element you are looking for.

If you want to add a new child node to a node: first use .Element to get the node: then use the .Add method on that node:
Hospitalizations.Descendants.Element("Hospitalization1").Add(new XElement("List of Procedures"));

如Addy Tas所建议,XPath(System.Linq.XPath)可能在此处可用于访问元素:[ ^ ].

As suggested by Addy Tas, XPath (System.Linq.XPath) may be useful here for accessing elements:[^].