Query XDocument with xmlns attribute (namespace)

前端 未结 2 2057
广开言路
广开言路 2020-12-06 10:08

I try to query elements from an visual studio *.csproj file. I created a short example to illustrate the problem:

    // Working
    string xml1 = @\"

        
2条回答
  •  我在风中等你
    2020-12-06 10:49

    It is not necessary to know the namespace beforehand. You can write code, that works with both Xmls because you can get the default namespace from XElement.

    XDocument doc = XDocument.Parse(xml2);
    XNamespace ns = doc.Root.GetDefaultNamespace();
    foreach (XElement element in doc.Descendants(ns + "ItemGroup"))
    {
        Console.WriteLine(element);
    }
    

    I also wrote an extension method to resolve the XName from any XObject (XElement, XDocument, etc.).

    The benefit in using the extension method instead of GetDefaultNamespace is that you don't have to check if there is already another Namespace provided.

    public static XName ResolveName(this XObject xObj, XName name)
    {
        //If no namespace has been added, use default namespace anyway
        if (string.IsNullOrEmpty(name.NamespaceName))
        {
            name = xObj.Document.Root.GetDefaultNamespace() + name.LocalName;
        }
        return name;
    }
    

    You can use it like this

    XDocument doc = XDocument.Parse(xml2);
    foreach (XElement element in doc.Descendants(doc.ResolveName("ItemGroup")))
    {
        Console.WriteLine(element);
    }
    

    I think LINQ to XML is a wonderful API. But I think it is clear, that if I don't provide a namespace, that I always mean the default namespace. I don't see any reason why LINQ to XML does not behave this way. This is a little drawback that really annoyed me. And the first time as a beginner with LINQ to XML I didn't know what I did wrong for hours when I forgot to provide the default namespace.

提交回复
热议问题