Question

In reference to this other question

Read an XML node value using Linq

What is the query doing when you specify the namespace? I find my query doesn't yield anything if I don't provide it. But when I do it works? How is it doing this? WHy does it work?

            //name
            XNamespace ns = "urn:test-test:v10";
            var name = root.Descendants(ns + "name");

            foreach (var result in name)
            {

            }
Était-ce utile?

La solution

XML Namespaces help you avoid name conflicts in XML Elements.

e.g. If your XML describes 2 different entities like Fruit & Animal, both will have a Name attribute.

When you render your XML, it would be great if your Fruit related elements are under a particular namespace. (defined by xmlns:value)

Once you do this, you cannot just identify a Name element using the Name alone. You need the enclosing namespace. This is what the LINQ Query does**. It helps you uniquely identify the element by using its full and correct name.

http://www.w3schools.com/xml/xml_namespaces.asp

Autres conseils

It's because the XML namespace is part of the full name of the element. If you specify the wrong name, you won't find the element.

If you specify the wrong namespace, you're specifying the wrong name, and so you won't find the element.

XML Namespaces prevent duplicate elements in a document, for example, if you you don't own the entire contents of the xml document.

XML Namespace tutorial

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top