以下是我的Sample XML:
<Response>
<Transactions>
<Upload>
<DocumentID>1</DocumentID>
<Name>A</Name>
<DocumentID>2</DocumentID>
<Name>B</Name>
</Upload>
<Upload>
<DocumentID>3</DocumentID>
<Name>C</Name>
<DocumentID>4</DocumentID>
<Name>D</Name>
</Upload>
<Upload>
<DocumentID>5</DocumentID>
<Name>E</Name>
<DocumentID>6</DocumentID>
<Name>F</Name>
</Upload>
<Transactions>
</Response>
下面给出的是我的输出:
<Total>
<DocumentID>1</DocumentId>
<DocumentID>2</DocumentId>
<DocumentID>3</DocumentId>
<DocumentID>4</DocumentId>
<DocumentID>5</DocumentId>
<DocumentID>6</DocumentId>
</Total>
我使用以下给定代码来获取输出:
var downloadfolder = @"C:\Users\KUMARMOHANTA\Desktop\New folder\test.xml";
var masterfile = new XDocument();
XElement newDocument = new XElement("Total");
masterfile.Add(newDocument);
XDocument xdoc = XDocument.Load(downloadfolder);
masterfile.Root.Add(xdoc.Descendants("DocumentId"));
masterfile.Save(@"C:\Users\KUMARMOHANTA\Desktop\merged_.xml");
但是我想要的输出应该是这样的:
<Total>
<Transactions>
<Upload>
<DocumentID>1</DocumentID>
<DocumentID>2</DocumentID>
</Upload>
<Upload>
<DocumentID>3</DocumentID>
<DocumentID>4</DocumentID>
</Upload>
<Upload>
<DocumentID>5</DocumentID>
<DocumentID>6</DocumentID>
</Upload>
<Transactions>
</Total>
答案 0 :(得分:1)
将元素Transactions
重命名为Total
。删除名为Name
的元素。然后保存文件。
试试这个:
var downloadfolder = @"C:\Users\KUMARMOHANTA\Desktop\New folder\test.xml";
XDocument xdoc = XDocument.Load(downloadfolder);
xdoc.Root.Name = "Total";
xdoc.Descendants("Upload").Elements("Name").Remove();
xdoc.Save(@"C:\Users\KUMARMOHANTA\Desktop\merged_.xml");
请注意,您的XML示例不会关闭Transactions
元素。