LINQ to XML - accessing descendants with a prefix

前端 未结 3 2180
太阳男子
太阳男子 2020-12-16 18:42

I have a sample xml file like this


Name
address
Zip         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 19:38

    You need to know what the namespace is. That will have been declared earlier, with something like:

    xmlns:vs="http://some_url_here"
    

    You can query using XNamespace:

    XNamespace vs = "http://some_url_here";
    var names = doc.Descendants(vs + "Name")
                   .Select(x => (string) x)
                   .ToList();
    

    The + here is actually converting an XNamespace and a string to an XName.

提交回复
热议问题