none
xml:lang attribute is failing

    Question

  • NameGram = new XDocument("grammar",new XAttribute("xml:lang","en-US"),new XAttribute("root","TOPLEVEL"),

    I heed to include the above line in my xml form that is being built via LINQ/  I have tried using the special "namespace xml" attribut but the runtime engine still does not like the colon.  The error being thrown is "the colon character, hex 03A si not allowed in a name.  I need to include the attribute within the XDocument declaration.  Any suggestions?

    Saturday, July 11, 2009 1:20 PM

Answers

  • Simply use new XAttribute(XNamespace.Xml + "lang", "en-US") to construct the attribute. With LINQ to XML element or attribute names never contain colons, instead you construct a name as a concatenation of an XNamespace and a local name. The serializer will then add any prefix necessary and in the case of XNamespace.Xml defaults to the prefix 'xml'.
    Here is an example:
                XNamespace xhtml = "http://www.w3.org/1999/xhtml";
                XNamespace xml = XNamespace.Xml;
                XDocument doc =
                    new XDocument(
                        new XElement(xhtml + "html",
                            new XAttribute(xml + "lang", "en"),
                            new XElement(xhtml + "head",
                                new XElement(xhtml + "title", "This is an example"))));
                doc.Save(Console.Out);
    Result is

    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>This is an example</title>
      </head>
    </html>
    As you can see, the attribute with local name 'lang' and the XNamespace.Xml namespace automatically has the proper prefix 'xml' when the XDocument is serialized/saved.

    MVP XML My blog
    • Proposed as answer by Martin Honnen Saturday, July 11, 2009 3:20 PM
    • Marked as answer by mreynol6 Sunday, July 12, 2009 6:51 PM
    Saturday, July 11, 2009 1:45 PM
  • Hi,

    Something like this should do it:
    XDocument doc = new XDocument(
      new XElement("grammar",
        new XAttribute(XNamespace.Xml + "lang", "en-US"),
        new XAttribute("root", "TOPLEVEL"),
        new XElement("rule",
          new XAttribute("id", "TOPLEVEL"),
          new XAttribute("scope", "public"),
          new XElement("item") // Fill in the item here
        ),
      )
    );
    It's not complete, but should give you a starting point.
    Thanks,
    Vitek Karas [MSFT]
    • Marked as answer by mreynol6 Sunday, July 12, 2009 6:50 PM
    Saturday, July 11, 2009 11:21 PM
    Moderator

All replies

  • Simply use new XAttribute(XNamespace.Xml + "lang", "en-US") to construct the attribute. With LINQ to XML element or attribute names never contain colons, instead you construct a name as a concatenation of an XNamespace and a local name. The serializer will then add any prefix necessary and in the case of XNamespace.Xml defaults to the prefix 'xml'.
    Here is an example:
                XNamespace xhtml = "http://www.w3.org/1999/xhtml";
                XNamespace xml = XNamespace.Xml;
                XDocument doc =
                    new XDocument(
                        new XElement(xhtml + "html",
                            new XAttribute(xml + "lang", "en"),
                            new XElement(xhtml + "head",
                                new XElement(xhtml + "title", "This is an example"))));
                doc.Save(Console.Out);
    Result is

    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>This is an example</title>
      </head>
    </html>
    As you can see, the attribute with local name 'lang' and the XNamespace.Xml namespace automatically has the proper prefix 'xml' when the XDocument is serialized/saved.

    MVP XML My blog
    • Proposed as answer by Martin Honnen Saturday, July 11, 2009 3:20 PM
    • Marked as answer by mreynol6 Sunday, July 12, 2009 6:51 PM
    Saturday, July 11, 2009 1:45 PM
  • Martin,
    Thank you for your thorough reply.  As always, you are right there to help and it is greatly appreciated.  The xml document that I am constucting is a voice grammar.  Here is a small portion of the document.  Do you think that you can modify your example to produce the top piece of this document?  The part that is throwing me is the "<grammar..." root element.

    <?xml version= "1.0"?>
      <grammar xml:lang="en-US" root = "TOPLEVEL">
        <rule id="TOPLEVEL" scope="public">
          <item>

        <!-- FIRST NAME RETURN -->

            <item repeat="0-1">
              <ruleref uri="#FIRSTNAME"/>
              <tag>out.firstNameSlot=rules.FIRSTNAME.firstNameSubslot</tag>
            </item>
     
        <!-- LAST NAME RETURN -->

              <ruleref uri="#LASTNAME"/>
              <tag>out.lastNameSlot=rules.LASTNAME.lastNameSubslot</tag>
            </item>

      <!-- TOP LEVEL RETURN-->
          <tag> out.F_1= out.firstNameSlot + out.lastNameSlot </tag>
        </rule>

      <rule id="FIRSTNAME" scope="public">
      <one-of>
        <item> matt<tag>out.firstNameSubslot="matthew";</tag></item>
        <item> dee <tag> out.firstNameSubslot="dee ";</tag></item>
        <item> jon <tag> out.firstNameSubslot="jon ";</tag></item>
        <item> george <tag>out.firstNameSubslot="george ";</tag></item>
        <item> billy <tag> out.firstNameSubslot="billy ";</tag></item>
      </one-of>
      </rule>

    Saturday, July 11, 2009 9:40 PM
  • Hi,

    Something like this should do it:
    XDocument doc = new XDocument(
      new XElement("grammar",
        new XAttribute(XNamespace.Xml + "lang", "en-US"),
        new XAttribute("root", "TOPLEVEL"),
        new XElement("rule",
          new XAttribute("id", "TOPLEVEL"),
          new XAttribute("scope", "public"),
          new XElement("item") // Fill in the item here
        ),
      )
    );
    It's not complete, but should give you a starting point.
    Thanks,
    Vitek Karas [MSFT]
    • Marked as answer by mreynol6 Sunday, July 12, 2009 6:50 PM
    Saturday, July 11, 2009 11:21 PM
    Moderator
  • Vitek,
    Many thanks for your suggestion.  And my thanks to Martin as well.  You both steered me to the answer and it works like a charm now.
    Sunday, July 12, 2009 6:51 PM