c# – Colon character in string


When working with xml files in C #, it was required to create the following element via linq2xml:


However, when trying to add it programmatically

new XAttribute("xml:space", "preserve")

I get this error:

"Sign": ", hex value 0x3A, cannot be used in names."

How can you add a string like "x: y"?

Slightly more complete code:

xdoc.Element("root").Add(new XElement("data", new XAttribute("name", x.Key), new XAttribute("xmlspace", "preserve"),new XElement("value", x.Value)));

// Надо xml:space

If anyone is asking the question, "WTF is that?", The answer is, programmatically adding localization resources to Visual Studio. (Originally taken via linq2xml from a Java resource file into a Dictionary and then converted into a Visual Studio resource file), and, yes, I know a lot about perversions.


And if so:

XmlDocument.CreateElement("prefix", "name", "uri"); //для элементов
new XAttribute(XNamespace.Xml + "space", "preserve"); //для атрибутов
Scroll to Top