C# check an element exists while using LINQ to XML

前端 未结 6 1011
慢半拍i
慢半拍i 2021-01-02 08:32

OK, bit of a random question, but the best way to do this is to just add the code, you\'ll be able to see what I mean straight away:

XML:



        
6条回答
  •  萌比男神i
    2021-01-02 09:00

    Should also be able to clean this kinda stuff up with extensions, something like..

    public string Element_valStr(XElement xElm, string xName)
    {
        if (xElm.Element(xName) == null) return string.empty;
        return xElm.Element(xName).Value;
    }
    

    and then just:

    var superType = (from c in linquee.Descendants("customer")  
                      where (c.Element_valStr("SuperType") == "1")
                      select c).ToList();
    

提交回复
热议问题