locked
Linq to Xml and namespaces RRS feed

  • Question

  • Hello all,

    Recently i've learned some Linq to XML and i have the following question that is not clear for me :

    If i have this :

    private XNamespace NAMESPACE = @"http://exampleSite.com";
    
    //....
    
     new XElement(NAMESPACE + "MyElement")
    

    How knows the Xml engine to return <nms:MyElement/>

    Basically what i want to know is how the translation between http://exampleSite.com and nms is performed by the xml engine (i've seen it works also without an internet connection)

    Also how it is possible for me to define my custom namespace that is recognized by the xml engine ?

    Also if you have additional free learning materials related to these topics can you please provide them ?

    Wednesday, January 15, 2014 11:25 AM

Answers

  • Hi,

    a Xml namespace has a similar purpose as a C# namespace. It is used to distinguish elements and attributes with the same name. The Xml engine will accept any URI - to guarantee its uniqueness you will have to use registered domain name as part of the namespace.

    See Understanding XML Namespaces and regarding LINQ To Xml: Working with XML Namespaces.

    Regards, Elmar

    • Marked as answer by florea.lucian Saturday, January 18, 2014 9:42 AM
    Wednesday, January 15, 2014 2:52 PM

All replies

  • Hi,

    a Xml namespace has a similar purpose as a C# namespace. It is used to distinguish elements and attributes with the same name. The Xml engine will accept any URI - to guarantee its uniqueness you will have to use registered domain name as part of the namespace.

    See Understanding XML Namespaces and regarding LINQ To Xml: Working with XML Namespaces.

    Regards, Elmar

    • Marked as answer by florea.lucian Saturday, January 18, 2014 9:42 AM
    Wednesday, January 15, 2014 2:52 PM
  • Hello Elmar,

    The links you provided helped me answer my question and also i learned some tricks 

    Thank you,

    Lucian.

    Saturday, January 18, 2014 9:44 AM