如何使用VB.NET代码将多个XML文件合并为一个。 [英] How do I merge multiple XML files into one using VB.NET code.

查看:148
本文介绍了如何使用VB.NET代码将多个XML文件合并为一个。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   root  >  
< purchase > < / purchase >
< sales > < / sales >
< / root >
< root >





我想要这样,因为目前所有这些xml都不同而且我希望他们合并为一个xml

 <   root  >  
< 购买 > < / purchase >
< sales > < / sales >
< / root >

< root >
< 购买 > < / purchase >
< sales > < / sales >
< / root >



合并完所有xml后我想创建一个csv文件。



我尝试过:



For每个xml在csvXmlList中

Dim dsTemp As New DataSet()

dsTemp.ReadXml(xml)

dsAll.Merge(dsTemp)

下一步

解决方案

好吧,没有简单的wa要合并xml文档。为什么?你必须完全确定所需的内容是否正常(在结构和价值方面)。



让我在下面的例子中解释一下:

  //   doc#1的内容 
string xcontent1 = @ < root>
<项目ID ='1'>
<子项>
< Subitem sid ='1'> A< / Subitem>
< Subitem sid ='2' > B< / Subitem>
< / Subitems>
< / Item>
< Item id ='2'>
< Subitems>
< Subitem sid ='1'> A< / Subitem>
< Subitem sid ='2'> B< / Subitem>
< Subitem sid ='3'> C< ; / Subitem>
< / Subitems>
< / Item>
< / root>
;
// doc#2的内容
字符串 xcontent2 = @ < root>
< Item id =' 1'>
<子项>
< Subitem sid ='1'> A< / Subitem>
< Subitem sid ='2'> E< / Subitem>
< Subitem sid ='3'> F< / Subitem>
< / Subitems>
< / Item>
< Item id ='3'> ;
<子项>
< Subitem sid ='1'> A< / Subitem>
< Subitem sid ='2'> B< / Subitem>
< Subitem sid ='3'> C< / Subitem>
< / Subitems>
< / Item>
< / root>
;

// 第一个文件
XDocument xdoc1 = XDocument.Parse (xcontent1);
// 第二个文件
XDocument xdoc2 = XDocument.Parse(xcontent2);
// 最终文件(合并)
XDocument xdst = new XDocument();
// 添加文档#1的全部内容
xdst.Add(xdoc1 。根);
// 添加没有root的文档#2的内容
xdst.Root。添加(xdoc2.Root.Elements());





合并文件的结果:

 <   root  >  
< 项目 id = 1 >
< 子项目 < span class =code-keyword>>
< Subitem sid = 1 > < / Subitem >
< Subitem sid = 2 > B < / Subitem >
< ; / Subitems >
< / Item >
< 项目 id = 2 >
< 子项 >
< Subitem sid = 1 > < / Subitem >
< Subitem sid = 2 > B < / Subitem >
< Subitem sid = 3 > C < / Subitem >
< / Subitems >
< / Item >
< span class =code-keyword>< 项目 id = 1 >
< 子项 >
< Subitem sid = 1 > < / Subitem >
< Subitem sid = 2 > E < / Subitem >
< Subitem sid = 3 > F < / Subitem >
< / Subitems >
< / Item >
< 项目 id = 3 >
< 子项目 >
< 子项 sid = 1 > < / Subitem >
< Subitem sid = 2 > B < / Subitem >
< Subitem sid = 3 < span class =code-keyword>> C < / Subitem >
< / Subitems >
< / Item >
< / root >





正如您所见,合并的xml文档包含2个项目具有相同 id (1)的节点,但内容不同(子项目)。有一条规则,你必须要尊重:程序员必须比用户更聪明。;)



结论:你必须提供方法将通过节点集合(及其子节点)来比较每个节点与当前导入的节点。


这不是一个xml文件

< pre lang =xml> < root >
< 购买 > ; < / purchase >
< sales > < / sales >
< / root >
< root > < / root >



但这是

 <   root  >  
< 购买 > < < span class =code-leadattribute> / purchase >
< 销售 > < / sales >
< / root >





当您合并2个xml文件时,您需要知道根文件在结果文件中是唯一的。

因此,至少生成的文件看起来像

 <   root  >  
< 购买 > < / purchase >
< 销售 > < / sales >
< / root >
< root > < / root >
< < span class =code-leadattribute> root > < / root >
< 购买 > < < span class =code-leadattribute> / purchase >
< 销售 > < < span class =code-leadattribute> / sales >

< root > < ; / root >



正如你所看到的,合并2个xml文件不仅仅是连接它们。


我通过在一些地方挖掘更多的东西得到了解决方案,它正在合理地合并多个XML文件正如所料。我在VB.NET中使用了XElement来完成这个任务。



要求: - 导入: -

  Imports  System.Xml .Linq 





  Dim  xdoc < span class =code-keyword> As  XElement 
Dim xdoc2 As XElement =< root> < / root >



 对于 每个 xml  XmlList 
xdoc = XElement.Parse(xml)
xdoc2.Add( xdoc)

下一步


<root>
<purchase></purchase>
<sales></sales>
</root>
<root>



I want it like that because currently all these xmls are different and I want them to merge in one xml

<root>
<purchase></purchase>
<sales></sales>
</root>

<root>
<purchase></purchase>
<sales></sales>
</root>


After merging all the xml's I want to create one csv file from that.

What I have tried:

For Each xml In csvXmlList
Dim dsTemp As New DataSet()
dsTemp.ReadXml(xml)
dsAll.Merge(dsTemp)
Next

解决方案

Well, there's no simple way to merge xml documents. Why? You have to be completely sure that desired content will be OK (in aspect of structure and values).

Let me explain it on below example:

//content of doc #1
string xcontent1 = @"<root>
<Item id='1'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>B</Subitem>
	</Subitems>
</Item>
<Item id='2'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>B</Subitem>
		<Subitem sid='3'>C</Subitem>
	</Subitems>
</Item>
</root>";
//content of doc #2
string xcontent2 = @"<root>
<Item id='1'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>E</Subitem>
		<Subitem sid='3'>F</Subitem>
	</Subitems>
</Item>
<Item id='3'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>B</Subitem>
		<Subitem sid='3'>C</Subitem>
	</Subitems>
</Item>
</root>";

//first document
XDocument xdoc1 = XDocument.Parse(xcontent1);
//second document
XDocument xdoc2 = XDocument.Parse(xcontent2);
//final document (merged)
XDocument xdst = new XDocument();
//add entire content of document #1
xdst.Add(xdoc1.Root);
//add content of document #2 without root
xdst.Root.Add(xdoc2.Root.Elements());



Result of merged document:

<root>
  <Item id="1">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">B</Subitem>
    </Subitems>
  </Item>
  <Item id="2">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">B</Subitem>
      <Subitem sid="3">C</Subitem>
    </Subitems>
  </Item>
  <Item id="1">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">E</Subitem>
      <Subitem sid="3">F</Subitem>
    </Subitems>
  </Item>
  <Item id="3">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">B</Subitem>
      <Subitem sid="3">C</Subitem>
    </Subitems>
  </Item>
</root>



As you can see, merged xml document contains 2 Item nodes with the same id (1), but with different content (Subitems). There's one rule, which you have to respect: Programmers have to be wiser than users. ;)

Conclusion: You have to provide method which will go through the nodes collection (and their subnodes) to be able to compare each node with currently imported node.


This is not an xml file

<root>
    <purchase></purchase>
    <sales></sales>
</root>
<root></root>


But this is

<root>
    <purchase></purchase>
    <sales></sales>
</root>



When you merge 2 xml files, you need to know that the root is unique in resulting file.
So at least the resulting file can look like

<root>
    <purchase></purchase>
    <sales></sales>
</root>
<root></root>
<root></root>
    <purchase></purchase>
    <sales></sales>

<root></root>


As you can see merging 2 xml files is not just concatenating them.


I got the solution by digging some more into few places and it is merging multiple XML files amazingly as expected. I have used XElement in VB.NET to accomplish this.

Requirement:- import :-

Imports System.Xml.Linq



Dim xdoc As XElement
Dim xdoc2 As XElement = <roots></roots>


For Each xml In XmlList
           xdoc = XElement.Parse(xml)
           xdoc2.Add(xdoc)

Next


这篇关于如何使用VB.NET代码将多个XML文件合并为一个。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆