从父节点信息创建XML
我有一个 xml 文件,其中包含根信息,如下所示: usequery 属性有一个 SQL 查询,从中填充 XML 元素数据。
<ARAXmlFormat>
<root name="level1" index = "1" parentid ="0" haschildren="yes"/>
<root name="level2" index = "2" parentid ="1" haschildren="yes" usequery="query2"/>
<root name="level21" index = "3" parentid ="2" haschildren="no" usequery="query1"/>
<root name="level22" index = "4" parentid ="2" haschildren="no" usequery="query3"/>
<root name="level3" index = "5" parentid ="1" haschildren="yes"/>
<root name="level31" index = "6" parentid ="5" haschildren="no" usequery="query4"/>
</ARAXmlFormat>
由此我需要生成一个 XML 树,如下所示。截至目前,我已经拥有 level2、level21、level22、level31 的单独 XElement。但是,如何创建 XML,并根据上面的 ParentID 信息以 XML 格式添加这些元素,如下所示?
<level1>
<level2>
<level21 attrib1 ="val1" attrib2="val2"/>
<level22 attrib1 ="val1" attrib2="val2"/>
</level2>
<level3>
<level31 attrib1 ="val1" attrib2="val2"/>
</level3>
</level1>
I have an xml file that has information of the roots as follows:
usequery attribute has a SQL query from which the XML element data is populated from.
<ARAXmlFormat>
<root name="level1" index = "1" parentid ="0" haschildren="yes"/>
<root name="level2" index = "2" parentid ="1" haschildren="yes" usequery="query2"/>
<root name="level21" index = "3" parentid ="2" haschildren="no" usequery="query1"/>
<root name="level22" index = "4" parentid ="2" haschildren="no" usequery="query3"/>
<root name="level3" index = "5" parentid ="1" haschildren="yes"/>
<root name="level31" index = "6" parentid ="5" haschildren="no" usequery="query4"/>
</ARAXmlFormat>
From this I need to generate an XML tree as follows. As of now I already have individual XElements for leve2, level21, level22, level31. But how do I create XML but adding these elements in the XML format as below from the parentid information above?
<level1>
<level2>
<level21 attrib1 ="val1" attrib2="val2"/>
<level22 attrib1 ="val1" attrib2="val2"/>
</level2>
<level3>
<level31 attrib1 ="val1" attrib2="val2"/>
</level3>
</level1>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
基本上,遍历元素,并为每个元素在字典中找到父级,将其作为子级添加到该父级,最后将其添加到字典中,以便它本身可以成为父级。这假设父级总是在其所有子级之前声明。
使用您的源数据,它会创建以下结果:
You can do it like this:
Basically, walk through the elements, and for each find the parent in a dictionary, add it as a child to that parent and finally add it to the dictionary, so that it can be parent itself. This assumes that parent is always declared before all its children.
With your source data, it creates the following result:
我不知道
attrib1
和attrib2
属性的值从何而来。但就输出 XML 树的层次结构而言,您可以使用类似以下内容递归地构造它:该解决方案的工作方式与输入文件中
元素的顺序无关。selectChildNodes
是一个委托,它接受父节点的索引作为输入参数,并递归地返回该元素的所有子节点。输出将如下所示,没有属性:使用
new XElement
构造每个节点时,可以包含节点属性的值。I don't know where the value of
attrib1
andattrib2
attributes come from. But as far as the hierarchical structure of the output XML tree is concerned, you could construct it recursively, using something like this:This solution works independent of order of
<root>
elements in the input file.selectChildNodes
is a delegate that accepts the index of parent node as the input parameter and returns all child nodes of that element recursively. The output would be like this, without attributes:The value of node attributes could be included when constructing each node with
new XElement
.