程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 創建xml

C# 創建xml

編輯:C#入門知識

學習xml筆記,下列為核心代碼片段,測試結果在附件xml文間中。
XmlDocument xmlDocument = new XmlDocument(); //create xml document
XmlNode xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", ""); //xml document header declaration
xmlDocument.AppendChild(xmlNode); // add xml document header declarations
XmlElement xmlElement = xmlDocument.CreateElement("", "TestDataModels", ""); //create xml root node
XmlElement element = xmlDocument.CreateElement("", "TestDataModels", ""); //create TestDataModels' child node
for (int i = 1; i < 6; i++)
{
XmlElement element = xmlDocument.CreateElement("", "TestDataModels", ""); //create TestDataModels' child node
//add child node for the TestDataModels's node
XmlElement elementCode = xmlDocument.CreateElement("Test", "Code", "");
XmlText xmlTextCode = xmlDocument.CreateTextNode(tm.TCode);
elementCode.AppendChild(xmlTextCode);
element.AppendChild(elementCode);

XmlElement elementName = xmlDocument.CreateElement("Test", "Name", "");
XmlText xmlTextName = xmlDocument.CreateTextNode(tm.TName);
elementName.AppendChild(xmlTextName);
element.AppendChild(elementName);

//testType start
XmlElement elementType = xmlDocument.CreateElement("Test", "Type", "");
XmlElement elementTypeCode = xmlDocument.CreateElement("Type", "TCode", "");
XmlText xmlTextTypeCode = xmlDocument.CreateTextNode(tt.TtCode);
elementTypeCode.AppendChild(xmlTextTypeCode);
elementType.AppendChild(elementTypeCode);

XmlElement elementTypeName = xmlDocument.CreateElement("Type", "TName", "");
XmlText xmlTextTypeName = xmlDocument.CreateTextNode(tt.TtName);
elementTypeName.AppendChild(xmlTextTypeName);
elementType.AppendChild(elementTypeName);
element.AppendChild(elementType);

//testType end
xmlElement.AppendChild(element); //For xmlElement add child element
}
xmlDocument.AppendChild(xmlElement); //For xmlDocument add child node
xmlDocument.Save(dataFile); //save the xmlDocument

本文出自 “狼之魂” 博客

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved