简体   繁体   中英

System.Xml.XmlException: „The ':' character, hexadecimal value 0x3A, cannot be included in a name.”

I wanna to create in my xml something like this:

<root>
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  </xsd:schema>
</root>

My code looks like this right now:

XElement rootElement = new XElement("root");
XDeclaration xDeclaration = new XDeclaration("1.0", "utf-8", "true");
XElement xdsElement = new XElement("xsd:schema"); // Exception there!

And my exception:

System.Xml.XmlException: „The ':' character, hexadecimal value 0x3A, cannot be included in a name.”

How to awoid this exception, maybe there is some kind of generator for that phrases?

As @juharr said in the comments, you need to define it as XNamespace first, here's how:

var ns_xsd = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
var ns_msdata = XNamespace.Get("urn:schemas-microsoft-com:xml-msdata");

var root = new XElement("root", 
    new XElement(ns_xsd + "schema", 
        new XAttribute("id", "root"), 
        new XAttribute("xmlns", ""), 
        new XAttribute(XNamespace.Xmlns + "xsd", ns_xsd),
        new XAttribute(XNamespace.Xmlns + "msdata", ns_msdata),
        new XAttribute(ns_msdata + "IsDataSet", "true")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM