C# - Linq to XML - Exclude elements from query

我是研究僧i 提交于 2019-12-12 00:48:21

问题


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>

回答1:


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));



回答2:


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.




回答3:


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")); 



回答4:


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);
}


来源:https://stackoverflow.com/questions/12838207/c-sharp-linq-to-xml-exclude-elements-from-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!