The ‘:’ character, hexadecimal value 0x3A, cannot be included in a name

The ‘:’ character, hexadecimal value 0x3A, cannot be included in a name

Asked on January 12, 2019 in XML.
Add Comment


  • 7 Answer(s)

         To use namespaces, LINQ to XML will makes it by simple:

    XNamespace ab = "http://whatever-the-url-is";
    XElement tempElement = doc.Descendants(ab + "test").FirstOrDefault();
    

         Search for an xmlns:ab=… section in the document to find that which namespace URI “ab” will refers to.

    Answered on January 12, 2019.
    Add Comment

         Trying to put the namespace in {} like,

    string xfaNamespace = "{http://www.xfa.org/schema/xfa-template/2.6/}";
    
    Answered on January 12, 2019.
    Add Comment

    Here is the solution:

    var ab = "http://whatever-the-url-is";
    

         here, ab was said to be a string. It caused the error reported by OP. In behalf of using the VAR keyword, here used the actual data type XNamespace.

    XNamespace ab = "http://whatever-the-url-is";
    
    Answered on January 12, 2019.
    Add Comment

    1 .var ns = doc.Root.Name.Namespace;

    2. XElement tempElement = doc.Descendants(XName.Get("test", "ab")).FirstOrDefault();

    Answered on January 13, 2019.
    Add Comment

    It’s for XML Namespace. If an element type name or attribute name contains a colon, then the mapping treats the part of the name before the colon as a prefix (of Namespace), and the part of the name after the colon as the local name.

    If Abc is your namespace your should use this code

    XNamespace aw = "Abc";
    XElement root = new XElement(aw + "Xyz", string.Empty);
    Answered on February 25, 2019.
    Add Comment
    Question
    Sign in to vote

    If you get the element names from external source, do you know their namespace definitions?

    I think in your sample element name “Abc:Xyz”, the “Abc” is just the namespace prefix, and you need to know what does “Abc” refer to. For example, in the original XML document, if there’s “xmlns:Abc=’http://www.abc.com'” then you should programmatically split the element names by possible “:”, and translate former part (namespace prefix) to its definition, and create XElement using namespace definition:

    XNamespace aw = "http://www.abc.com";
    XElement root = new XElement(aw + "Xyz", string.Empty);
    

    If you don’t know the namespace definition from external source, I think you can just ignore the namespace in your XElement, and set its element name to be “Xyz” only.

    Answered on February 25, 2019.
    Add Comment
    Question
    Sign in to vote

    If you want a prefix in the name then you can achieve that if you know what namespace you want to bind the prefix to. Here is an example:

                XNamespace ns1 = "http://example.com/ns1";
                string prefix = "pf1";
                string localName = "foo";
                XElement el = new XElement(ns1 + localName, new XAttribute(XNamespace.Xmlns + prefix, ns1));
                Console.WriteLine(el);

    Its output is

    <pf1:foo xmlns:pf1="http://example.com/ns1" />

    As you can see, the name passed to the XElement constructor is the concatenation of the XNamespace object ns1 and the localName, then an XML namespace declaration attribute is added that ensures that the prefix is bound to the XNamespace ns1 URI.

    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.