问题描述
您好
我使用DataSet.WriteToXML()来获取xml文件,输出如下:
I am using DataSet.WriteToXML() to get the xml file, the output is as follows:
  ;
<?xml version =" 1.0"编码= QUOT; UTF-8英寸standalone =" yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
< temp>
< temp>
<参考> S1002< /参考>
< CHID> 1100< / CHID>
< Number> 1< / Number>
< ItmCnt> 31< / ItmCnt>
< val> 920617< / val>
< cc> bpp< / cc>
< / temp>
<temp>
<temp>
<Reference>S1002</Reference>
<CHID>1100</CHID>
<Number>1</Number>
<ItmCnt>31</ItmCnt>
<val>920617</val>
<cc>bpp</cc>
</temp>
< mob>
< BI> r1< / BI>
< ; CdtDbtInd> C< / cdtDbtInd>
< Amt> 200000< / Amt>
< / mob>
<mob>
<BI>r1</BI>
<CdtDbtInd>C</CdtDbtInd>
<Amt>200000</Amt>
</mob>
< ; mob>
< BI> r2< / BI>
< CdtDbtInd> C< / cdtDbtInd>
< Amt> 100000< ; / Amt>
$
< / mob>
<mob>
<BI>r2</BI>
<CdtDbtInd>C</CdtDbtInd>
<Amt>100000</Amt>
</mob>
< / temp>
</temp>
但我希望它为:
<?xml version =" 1.0"编码= QUOT; UTF-8英寸standalone =" yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
< body>
<body>
< temp>
  ;&NBSP; <参考> S1002< /参考>
< CHID> 1100< / CHID>
< Number> 1< / Number>
< ItmCnt> 31< / ItmCnt>
< val> 920617< / val>
< cc> bpp< / cc>
< mob>
< BI> r1< / BI>
< CdtDbtInd> C< / cdtDbtInd>
< Amt> 200000< / Amt>
< / mob>
<temp>
<Reference>S1002</Reference>
<CHID>1100</CHID>
<Number>1</Number>
<ItmCnt>31</ItmCnt>
<val>920617</val>
<cc>bpp</cc>
<mob>
<BI>r1</BI>
<CdtDbtInd>C</CdtDbtInd>
<Amt>200000</Amt>
</mob>
< mob>
< BI> r2< / BI>
< CdtDbtInd> C< / cdtDbtInd>
< Amt> 100000< / Amt>
< / mob>
<mob>
<BI>r2</BI>
<CdtDbtInd>C</CdtDbtInd>
<Amt>100000</Amt>
</mob>
< / temp>
</temp>
< ; / body>
</body>
如何删除第一个< temp>标签和< body>< / body>标签如上?
How can I remove the first<temp> tag and the <body></body> tag as above?
非常感谢帮助......
Really appreciate for helps...
推荐答案
using System.Xml.Linq;
// Original XML document created by dataset.
XDocument xdoc = XDocument.Load("File Path and Name.xml");
// The new reformatted XML document
XDocument xdocConverted = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("body"));
// Get the XML node to go next after the root element
var temp = xdoc.Root.Element("temp");
// Get the mob's nodes that will come next
var mobs = xdoc.Root.Elements("mob");
// Add mob nodes as child nodes of temp
temp.Add(mobs);
// Add the new formatted nodes to the new document
xdocConverted.Root.Add(temp);
// Save the document out to the file system
xdocConverted.Save("File Path and Name.xml");
这篇关于添加body元素并删除其他一些元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!