2016-04-29 68 views
0

我在C#中工作,我不知道如何創建一個帶有空格的xml節點。如何製作特殊的XML節點?

它shoult如下所示:

<Test1 unitTest="KGM">1234<Test1/> 
+0

你的意思attribure? – Marusyk

+0

是的,我認爲這是我正在尋找的。不知道我應該搜索哪個詞。 – xileb0

回答

3

你嘗試使用谷歌?

XmlElement test1 = doc.CreateElement("Test1"); 
test1.SetAttribute("unitTest", "KGM"); 
test1.InnerText = "1234"; 
+1

謝謝,這正是我需要的。 將在8分鐘內接受 – xileb0

+0

@ xileb0很高興有幫助 – Marusyk

+0

我用谷歌,但我不知道這個詞的屬性。抱歉。 – xileb0

1

這裏的簡化解決方案,你彷彿要詢問:

using System.Xml; 

... 

//Create XML Document 
XmlDocument xmlDoc = new XmlDocument(); 

//Crate a node and add it to document 
XmlNode node = xmlDoc.CreateElement("Test1"); 
xmlDoc.AppendChild(node); 

//Create an attribute and add it to node 
XmlAttribute attrib = xmlDoc.CreateAttribute("unitTest"); 
attrib.Value = "KGM"; 
node.Attributes.Append(attrib); 

//Add text content to node 
node.InnerText = "1324";