2015-05-11 2 views
0

Я пишу XML с C#, вот XML,C# записи Xml (Атрибуты с HTTP)

<?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, это не работает ... кто-нибудь знает, как написать эти атрибуты? Спасибо ...

+0

Вы создаете атрибуты неправильные. Имя атрибута не 'xmln', это пространство имен. Имя «xsi», а значение «http: // ...». –

+0

Заблуждение, что вы получаете здесь? –

+0

: Неожиданный символ 'http ' – Eleanor

ответ

1

Имена атрибутов: 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"))); 
+0

благодарит много ... – Eleanor

Смежные вопросы