<?xml version="1.0" standalone="yes" ?>
<Tree>
<TreeNode>
<NodeId>0</NodeId>
<Title>代號 </Title>
<NodeXmlSrc>Content.xml</NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId>0</NodeId>
<Title>品牌</Title>
<NodeXmlSrc>BrandList.xml</NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId>0</NodeId>
<Title>型體</Title>
<NodeXmlSrc>SpecContent.xml</NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId>0</NodeId>
<Title>客戶</Title>
<NodeXmlSrc>BuyerList.xml</NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId>0</NodeId>
<Title>規格</Title>
<NodeXmlSrc>SpecList.xml</NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId>0</NodeId>
<Title>中國</Title>
<NodeXmlSrc>aa.xml</NodeXmlSrc>
</TreeNode>
</Tree>
XML文件2
<?xml version="1.0" standalone="yes" ?>
<Tree>
<TreeNode NodeId="0" Title="客戶" NodeXmlSrc="BuyerList.xml"></TreeNode>
<TreeNode NodeId="0" Title="品牌" NodeXmlSrc="Sample.xml"></TreeNode>
<TreeNode NodeId="0" Title="型體" NodeXmlSrc="msdnlib587_.xml"></TreeNode>
<TreeNode NodeId="0" Title="規格" NodeXmlSrc="msdnlib3_.xml"></TreeNode>
<TreeNode NodeId="0" Title="代號" NodeXmlSrc="msdnlib3_.xml"></TreeNode>
<TreeNode NodeId="0" Title="產地" NodeXmlSrc="msdnlib587_.xml"></TreeNode>
<TreeNode NodeId="0" Title="中國" NodeXmlSrc="123456.xml"></TreeNode>
</Tree>
誰能可以給我一個這兩個文件互相轉換的代碼,其實它們的表結構是一樣的,只是形式不一樣,如何互換啊.請給出代碼和思路啊!up有分
up
一个是Attribute一个是Element,可以创建一个实体类来解析,很简单的方法是在.Net编辑环境中取得这两个xml文件的框架,生成类文件,直接操作。
将文件包含在一个工程中在文件上点右键,得到框架,在框架上右键,得到代码
你有Biztalk之类的工具吗?可以用Bitalk的mapping工具生成这两个xml之间转换的xslt,然后用xml dom中的方法直接调用.transfer,【具体方法名记得不是特别清楚了】就可以转换了,基本不用什么代码。
up
如果需要给我信箱,我的邮箱wj_wj_2001@163.com.
将文件包含在一个工程中在文件上点右键,得到框架,在框架上右键,得到代码
UP
从文件1到文件2
XmlDocument doc = new XmlDocument();
XmlDocument docnew = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlNode nodeRoot=doc.DocumentElement ;
docnew.LoadXml ("<"+nodeRoot.Name +"/>");
XmlNode nodeRootnew=docnew.DocumentElement ;
foreach(XmlNode node in nodeRoot.ChildNodes)
{
XmlNode n=docnew.CreateNode(XmlNodeType.Element ,node.Name,"" );
foreach(XmlNode n1 in node.ChildNodes )
{
XmlAttribute attr = docnew.CreateAttribute(n1.Name );
attr.Value =n1.InnerText .Trim ();
n.Attributes.SetNamedItem (attr);
}
nodeRootnew.AppendChild (n);
}
docnew.Save ("t.xml");
从XML2到XML1
XmlDocument doc = new XmlDocument();
XmlDocument docnew = new XmlDocument();
doc.Load("XMLFile2.xml");
XmlNode nodeRoot=doc.DocumentElement ;
docnew.LoadXml ("<"+nodeRoot.Name +"/>");
XmlNode nodeRootnew=docnew.DocumentElement ;
foreach(XmlNode node in nodeRoot.ChildNodes)
{
XmlNode n=docnew.CreateNode(XmlNodeType.Element ,node.Name,"" );
foreach(XmlAttribute atr in node.Attributes )
{
XmlNode nc=docnew.CreateNode(XmlNodeType.Element ,atr.Name ,"" );
nc.InnerText =atr.Value ;
n.AppendChild (nc);
}
nodeRootnew.AppendChild (n);
}
docnew.Save ("t1.xml");
用样式表将一个XML文档以拉的方式转换成另一个XML文档
帮你UP