學習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
本文出自 “狼之魂” 博客