Using the following example xml containing one duplicate:
<Persons>
<Person>
<PersonID>7506</PersonID>
<Forename>K</Forename>
<Surname>Seddon</Surname>
<ChosenName />
<MiddleName />
<LegalSurname />
<Gender>Male</Gender>
</Person>
<Person>
<PersonID>6914</PersonID>
<Forename>Clark</Forename>
<Surname>Kent</Surname>
<ChosenName>Clark</ChosenName>
<MiddleName />
<LegalSurname>Kent</LegalSurname>
<Gender>Male</Gender>
</Person>
<Person>
<PersonID>6914</PersonID>
<Forename>Clark</Forename>
<Surname>Kent</Surname>
<ChosenName>Clark</ChosenName>
<MiddleName />
<LegalSurname>Kent</LegalSurname>
<Gender>Male</Gender>
</Person>
</Persons>
I have the following code where I am trying to build an XDocument with the output of an XPath query that filters the duplicate elements:
string outputXml = null;
var original = XDocument.Parse(xmlString);
string xpathFilterDups = "//Person[not(PersonID = following::Person/PersonID)]";
XDocument people = new XDocument("Persons", original.XPathSelectElements(xpathFilterDups));
outputXml = people.ToString();
I get the error:
An exception of type 'System.ArgumentException' occurred in System.Xml.Linq.dll but was not handled in user code
Non white space characters cannot be added to content.
On this line:
XDocument people = new XDocument("Persons", original.XPathSelectElements(xpath));
What am I doing wrong? :-\
You can ignore pretty much all your code, the issue is just this:
You can't create an
XDocument
containing a string, you need to add an element: