Yoav
Yoav

Reputation: 2077

C# - Linq to XML - Exclude elements from query

I have this XML file:

<MyXml>
    <MandatoryElement1>value</MandatoryElement1>
    <MandatoryElement2>value</MandatoryElement2>
    <MandatoryElement3>value</MandatoryElement3>
    <CustomElement1>value</CustomElement1>
    <CustomElement2>value</CustomElement2>
<MyXml>

All 3 elements that are called 'MandatoryElementX' will always appear in the file. The elements called 'CustomElementX' are unknown. These can be added or removed freely by a user and have any name.

What I need is to fetch all the elements that are not MandatoryElements. So for the file above I would want this result:

<CustomElement1>value</CustomElement1>
<CustomElement2>value</CustomElement2>

I don't know what the names of the custom elements may be, only the names of the 3 MandatoryElements, so the query needs to somehow exclude these 3.

Edit:
Even though this was answered, I want to clarify the question. Here is an actual file:

<Partner>
    <!--Mandatory elements-->
    <Name>ALU FAT</Name>
    <InterfaceName>Account Lookup</InterfaceName>
    <RequestFolder>C:\Documents and Settings\user1\Desktop\Requests\ALURequests</RequestFolder>
    <ResponseFolder>C:\Documents and Settings\user1\Desktop\Responses</ResponseFolder>
    <ArchiveMessages>Yes</ArchiveMessages>
    <ArchiveFolder>C:\Documents and Settings\user1\Desktop\Archive</ArchiveFolder>
    <Priority>1</Priority>

    <!--Custom elements - these can be anything-->
    <Currency>EUR</Currency>
    <AccountingSystem>HHGKOL</AccountingSystem>
</Partner>

The result here would be:

<Currency>EUR</Currency>
<AccountingSystem>HHGKOL</AccountingSystem>

Upvotes: 0

Views: 970

Answers (4)

Madurika Welivita
Madurika Welivita

Reputation: 900

lets say TestXMLFile.xml will contain your xml,

 XElement doc2 = XElement.Load(Server.MapPath("TestXMLFile.xml"));



List<XElement> _list = doc2.Elements().ToList(); 

List<XElement> _list2 = new List<XElement>();

foreach (XElement x in _list)
{
    if (!x.Name.LocalName.StartsWith("Mandatory"))
    {

        _list2.Add(x);
    }
}


foreach (XElement y in _list2)
{
    _list.Remove(y);
}

Upvotes: 0

Gene S
Gene S

Reputation: 2773

Your question shows improperly formatted XML but I am assuming that is a typo and the real Xml can be loaded into the XDocument class.

Try this...

string xml = @"<MyXml> 
    <MandatoryElement1>value</MandatoryElement1> 
    <MandatoryElement2>value</MandatoryElement2> 
    <MandatoryElement3>value</MandatoryElement3> 
    <CustomElement1>value</CustomElement1> 
    <CustomElement2>value</CustomElement2> 
</MyXml> ";

System.Xml.Linq.XDocument xDoc = XDocument.Parse(xml);
var result = xDoc.Root.Descendants() 
                 .Where(x => !x.Name.LocalName.StartsWith("MandatoryElement")); 

Upvotes: 0

Tomtom
Tomtom

Reputation: 9394

Do you have created this xml or do you get it by another person/application? If it's yours I would advise you not to number it. You can do something like

<MyXml>     
   <MandatoryElement id="1">value<\MandatoryElement>
   <MandatoryElement id="2">value<\MandatoryElement>     
   <MandatoryElement id="3">value<\MandatoryElement>
   <CustomElement id="1">value<\CustomElement>
   <CustomElement id="2">value<\CustomElement> 
<MyXml> 

In the LINQ-Statement you don't need the List then.

Upvotes: 0

cuongle
cuongle

Reputation: 75306

You can define a list of mandatory names and use LINQ to XML to filter:

 var mandatoryElements = new List<string>() {
                   "MandatoryElement1", 
                   "MandatoryElement2", 
                   "MandatoryElement3"
                };

 var result = xDoc.Root.Descendants()
                  .Where(x => !mandatoryElements.Contains(x.Name.LocalName));

Upvotes: 2

Related Questions