C#写Xml(带http的属性)
C# write Xml (Attributes with http)
我正在用 c# 编写 xml,这里是 xml,
<?xml version="1.0" encoding="utf-16"?>
<game xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</game>
我的代码在这里:
XmlDocument doc = new XmlDocument();
XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-16", null);
doc.AppendChild(decl);
XmlElement game = doc.CreateElement("game");
doc.AppendChild(game);
XmlNode xmldocSelect = doc.SelectSingleNode("game");
//Crteate Attribute
XmlAttribute xmln = doc.CreateAttribute("xmln");
xmln.Value =":xsi="http://www.w3.org/2001/XMLSchema-instance"";
XmlAttribute xmlns = doc.CreateAttribute("xmlns");
xmlns.Value =":xsd="http://www.w3.org/2001/XMLSchema"";
xmldocSelect.Attributes.Append(xmln);
xmldocSelect.Attributes.Append(xmlns);
但是因为属性有http,所以不起作用...有人知道这些属性怎么写吗?
谢谢...
属性名称是 xsd
和 xsi
- xmlns
是规范中内置的名称空间前缀。
你会这样创建:
var doc = new XmlDocument();
var declaration = doc.CreateXmlDeclaration("1.0", "UTF-16", null);
doc.AppendChild(declaration);
var game = doc.CreateElement("game");
game.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
game.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
doc.AppendChild(game);
除非你有充分的理由坚持使用旧的 XmlDocument
API,否则我会使用 LINQ 到 XML:
var doc = new XDocument(
new XDeclaration("1.0", "UTF-16", null),
new XElement("game",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema")));
我正在用 c# 编写 xml,这里是 xml,
<?xml version="1.0" encoding="utf-16"?>
<game xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</game>
我的代码在这里:
XmlDocument doc = new XmlDocument();
XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-16", null);
doc.AppendChild(decl);
XmlElement game = doc.CreateElement("game");
doc.AppendChild(game);
XmlNode xmldocSelect = doc.SelectSingleNode("game");
//Crteate Attribute
XmlAttribute xmln = doc.CreateAttribute("xmln");
xmln.Value =":xsi="http://www.w3.org/2001/XMLSchema-instance"";
XmlAttribute xmlns = doc.CreateAttribute("xmlns");
xmlns.Value =":xsd="http://www.w3.org/2001/XMLSchema"";
xmldocSelect.Attributes.Append(xmln);
xmldocSelect.Attributes.Append(xmlns);
但是因为属性有http,所以不起作用...有人知道这些属性怎么写吗? 谢谢...
属性名称是 xsd
和 xsi
- xmlns
是规范中内置的名称空间前缀。
你会这样创建:
var doc = new XmlDocument();
var declaration = doc.CreateXmlDeclaration("1.0", "UTF-16", null);
doc.AppendChild(declaration);
var game = doc.CreateElement("game");
game.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
game.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
doc.AppendChild(game);
除非你有充分的理由坚持使用旧的 XmlDocument
API,否则我会使用 LINQ 到 XML:
var doc = new XDocument(
new XDeclaration("1.0", "UTF-16", null),
new XElement("game",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema")));