locked
xml attributes in c# with character ":" RRS feed

  • Question

  • Hi,

    i am using a XMLDocument to generate a XML file and some elements have an attribute name containing the caracter ":".
    The code is:

    elem = xmldoc.CreateElement("Livraria");

    elem.SetAttribute("Nome:XPTO", "TESTESARA");

    elem.SetAttribute("Local", "Braga");

    xmldoc.DocumentElement.AppendChild(elem);

    In the XML file generated the attribute name is only XPTO and i wanted it to be Nome:XPTO.

    Is this possible or is it a limitation of the framework?

    Thanks in advance.
    SP

    Friday, January 9, 2009 9:58 AM

Answers

  • Hi,

    Its not possible to add ":" in the Node, instead of that you can put "-", "_" such things along with the Node name.


    S. Ramkumar

    Smiley
    • Proposed as answer by Harry Zhu Tuesday, January 13, 2009 8:47 AM
    • Marked as answer by Harry Zhu Friday, January 16, 2009 6:26 AM
    Friday, January 9, 2009 12:32 PM
  • Hi,

    Please take a look at XML Namespaces.
    We can see a colon as a 'keyword'.If an element type name or attribute name contains a colon, then the part of the name before the colon is treated as a prefix, and the part of the name after the colon as the local name.

    An example in the document:
    <NAME HTML:CLASS="largeSansSerif" xmlns:HTML="http://www.w3.org/TR/REC-html40" >Layman, A</NAME>

    maps to

    <NAME {http://www.w3.org/TR/REC-html40}CLASS="largeSansSerif" >Layman, A</NAME>

    Best regards,
    Harry




    • Proposed as answer by Harry Zhu Tuesday, January 13, 2009 8:47 AM
    • Edited by Harry Zhu Tuesday, January 13, 2009 8:50 AM add
    • Marked as answer by Harry Zhu Friday, January 16, 2009 6:26 AM
    Tuesday, January 13, 2009 8:46 AM

All replies

  • Hi,

    Its not possible to add ":" in the Node, instead of that you can put "-", "_" such things along with the Node name.


    S. Ramkumar

    Smiley
    • Proposed as answer by Harry Zhu Tuesday, January 13, 2009 8:47 AM
    • Marked as answer by Harry Zhu Friday, January 16, 2009 6:26 AM
    Friday, January 9, 2009 12:32 PM
  • Hi,

    Please take a look at XML Namespaces.
    We can see a colon as a 'keyword'.If an element type name or attribute name contains a colon, then the part of the name before the colon is treated as a prefix, and the part of the name after the colon as the local name.

    An example in the document:
    <NAME HTML:CLASS="largeSansSerif" xmlns:HTML="http://www.w3.org/TR/REC-html40" >Layman, A</NAME>

    maps to

    <NAME {http://www.w3.org/TR/REC-html40}CLASS="largeSansSerif" >Layman, A</NAME>

    Best regards,
    Harry




    • Proposed as answer by Harry Zhu Tuesday, January 13, 2009 8:47 AM
    • Edited by Harry Zhu Tuesday, January 13, 2009 8:50 AM add
    • Marked as answer by Harry Zhu Friday, January 16, 2009 6:26 AM
    Tuesday, January 13, 2009 8:46 AM