I have the following code but it doesn't work. How can I make this work? The following code gives an exeption: newXdoc.Add(cComb); This operation would create an incorrectly structured document.
(Also thnx to Gert-Jan who provided the code for me partially)
private void button1_Click(object sender, EventArgs e)
{
var x1 = XDocument.Load(sourceFilepathTb.Text);
var x2 = XDocument.Load(targetFilepathTb.Text);
// select the CLASS nodes from each
var c1 = x1.Descendants("ActionDesign").First().Descendants("Action");
var c2 = x2.Descendants("ActionDesign").First().Descendants("Action");
// this one gives the distinct union of the two, you can
// put that into the result xdoc.
var cComb =
c1
.Union(c2)
.Distinct(new ClassComparer())
.OrderBy(c => c.Attribute("Id").Value);
XDocument newXdoc = new XDocument(
new XDeclaration("1", "utf-8", null),
new XElement("Application"));
//newXdoc.Add(new XElement(cComb));
//newXdoc.Add(new XDeclaration("1", "utf-8", "yes"));
newXdoc.Add(cComb);
You're trying to add several elements to the absolute root of the document, giving several root elements.
The easiest way to fix this is just to use:
newXdoc.Root.Add(cComb);
That will add the elements to the existing root element instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.