XML文档的简单读写

问题来源自CSDN社区:原贴见http://community.csdn.net/Expert/TopicView3.asp?id=5001302
楼主想做到的就是把XML Doc1 和 XML Doc2 合成为 XML Doc3 的样子,大概的效果就是把前几层结构相同的子项合并到一起.
示例文档XMLFILE1.XML
<?xml version="1.0" encoding="utf-8"?>
<root>
  <typekind text="食品">
    <typekindthinglist>
      <thing_id id="dd59927" title="月饼" url=""></thing_id>
      <thing_id id="59dddd7" title="豆沙包" url=""></thing_id>
    </typekindthinglist>
    <typekindchildtype>
      <typekind text="小菜">
        <typekindthinglist>
          <thing_id id="dd59927" title="花生米" url=""></thing_id>
          <thing_id id="59dddd7" title="蚕豆" url=""></thing_id>
        </typekindthinglist>
        <typekindchildtype>
        </typekindchildtype>
      </typekind>
    </typekindchildtype>
  </typekind>
</root>
示例文档XMLFILE2.XML
<?xml version="1.0" encoding="utf-8"?>
<root>
  <typekind text="食品">
    <typekindthinglist>
      <thing_id id="dd599271" title="月11饼" url="">
      </thing_id>
      <thing_id id="59dddd7" title="豆沙包" url="">
      </thing_id>
    </typekindthinglist>
    <typekindchildtype>
      <typekind text="主食">
        <typekindthinglist>
          <thing_id id="dd59927" title="米饭" url=""></thing_id>
          <thing_id id="59dddd7" title="面条" url=""></thing_id>
        </typekindthinglist>
        <typekindchildtype>
        </typekindchildtype>
      </typekind>
    </typekindchildtype>
  </typekind>
</root>
合并的结果文档XMLFILE3.XML
<?xml version="1.0" encoding="utf-8"?>
<root>
  <typekind text="食品">
    <typekindthinglist>
      <thing_id id="dd599271" title="月11饼" url="" />
      <thing_id id="59dddd7" title="豆沙包" url="" />
      <thing_id id="dd59927" title="月饼" url="" />
    </typekindthinglist>
    <typekindchildtype>
      <typekind text="主食">
        <typekindthinglist>
          <thing_id id="dd59927" title="米饭" url="" />
          <thing_id id="59dddd7" title="面条" url="" />
        </typekindthinglist>
        <typekindchildtype />
      </typekind>
      <typekind text="小菜">
        <typekindthinglist>
          <thing_id id="dd59927" title="花生米" url="" />
          <thing_id id="59dddd7" title="蚕豆" url="" />
        </typekindthinglist>
        <typekindchildtype />
      </typekind>
    </typekindchildtype>
  </typekind>
</root>
解决思路:
观察每个分类节点
都具有相似的结构
<typekind text="主食">
<typekindthinglist>
<thing_id id="dd59927" title="米饭" url=""></thing_id>
<thing_id id="59dddd7" title="面条" url=""></thing_id>
</typekindthinglist>
<typekindchildtype>
</typekindchildtype>
</typekind>
可简化为<typekind><typekindthinglist/><typekindchildtype/></typekind>
解决思路就是先找到每层的typekind列表
如果目标文档中存在路径相同text属性相同的typekind节点,则直接在此节点添加typekindthinglist和typekindchildtype节点集合,反之创建
对于typekindthinglist的节点本身亦然
如果typekingthinglist的thing_id集合中已有id属性相同的子节点,则不做任何处理(可能xmlfile1,xmlfile2中有相同路径相同id属性的thing_id,不认为是两种商品),反之添加thing_id节点
对于typekindchildtype结点下的每一个子节点,都是一个typekind
则递归重复以上过程
代码:

首先你的两个xml文档都不是良构的,麻烦改正确
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ConsoleApplication1
{
class Program
{
private static XmlDocument xd3;
static void Main(string[] args)
{
try
{
//读取第一个xml文档
XmlDocument xd1 = new XmlDocument();
xd1.Load("../../xmlfile1.xml");
XmlElement xe1 = xd1.DocumentElement;
//读取第二个xml文档
XmlDocument xd2 = new XmlDocument();
xd2.Load("../../xmlfile2.xml");
XmlElement xe2 = xd2.DocumentElement;
//构建新的文档
xd3 = new XmlDocument();
xd3.LoadXml ("<root></root>");
XmlElement xe3 = xd3.DocumentElement;
xd3.InsertBefore(xd3.CreateXmlDeclaration("1.0","utf-8",null),xe3);
CloneXmlNode(xe1, xe3);
CloneXmlNode(xe2, xe3);
xd3.Save("../../xmlfile3.xml");


}
catch (Exception excep)
{
Console.Write(excep.ToString());
}
finally {
Console.Read();
}
}

static void CloneXmlNode(XmlNode xnsour, XmlNode xndesc) {
//分类typekind列表
XmlNodeList xnl = xnsour.SelectNodes("typekind");
foreach (XmlNode xn in xnl) {
string text = xn.Attributes["text"].Value;
XmlNode xnt=xndesc.SelectSingleNode(String.Format("typekind[@text=/"{0}/"]", text));
if ( xnt == null) {
//如果找不到分类,创建分类
xnt = xd3.CreateElement("typekind");
XmlAttribute xatext=xd3.CreateAttribute("text");
xatext.Value = text;
xnt.Attributes.Append(xatext);
xndesc.AppendChild(xnt);
}
//叶列表
//typekindthinglist
XmlNode typekindthinglistSour = xn.SelectSingleNode("typekindthinglist");
XmlNode typekindthinglistDesc = xnt.SelectSingleNode("typekindthinglist");
if (typekindthinglistDesc == null) {
typekindthinglistDesc = xd3.CreateElement("typekindthinglist");
xnt.AppendChild(typekindthinglistDesc);
}
//读取叶节点
XmlNodeList thing_idSourS= typekindthinglistSour.SelectNodes("thing_id");
foreach (XmlNode thing_idSour in thing_idSourS) {
string id = thing_idSour.Attributes["id"].Value;
XmlNode thing_idDesc = typekindthinglistDesc.SelectSingleNode(String.Format("thing_id[@id=/"{0}/"]", id));
if (thing_idDesc == null)
{
//如果目标中不存在这样的叶节点,加入之
thing_idDesc = xd3.CreateElement("thing_id");
for (int i = 0; i < thing_idSour.Attributes.Count; i++) {
XmlAttribute xatemp = xd3.CreateAttribute(thing_idSour.Attributes[i].Name);
xatemp.Value = thing_idSour.Attributes[i].Value;
thing_idDesc.Attributes.Append(xatemp);
}
typekindthinglistDesc.AppendChild(thing_idDesc);
}
}
//子节点集合
//typekindchildtype
XmlNode typekindchildtypeSour = xn.SelectSingleNode("typekindchildtype");
XmlNode typekindchildtypeDesc = xnt.SelectSingleNode("typekindchildtype");
if (typekindchildtypeDesc == null)
{
typekindchildtypeDesc = xd3.CreateElement("typekindchildtype");
xnt.AppendChild(typekindchildtypeDesc);
}

if (typekindchildtypeSour != null) {

CloneXmlNode(typekindchildtypeSour, typekindchildtypeDesc);
}

}

}
}
}
 
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值