//XmlNode生成XML
XmlDocument xmlnDoc = new XmlDocument();
XmlNode xmlnRoot = xmlnDoc.CreateNode(XmlNodeType.Element, "nRoot", String.Empty);
XmlNode itemNode = null;
for (int i = 0; i < 3; i++)
{
itemNode = xmlnDoc.CreateNode(XmlNodeType.Element, "Customer", String.Empty);
XmlNode codeNode = xmlnDoc.CreateNode(XmlNodeType.Element, "Code", String.Empty);
codeNode.InnerText = "Code"+i.ToString();
XmlNode nameNode = xmlnDoc.CreateNode(XmlNodeType.Element, "Name", String.Empty);
nameNode.InnerText = "Name"+i.ToString();
itemNode.AppendChild(codeNode);
itemNode.AppendChild(nameNode);
xmlnRoot.AppendChild(itemNode);
}
xmlnDoc.AppendChild(xmlnRoot);
生成的结果为:
<nRoot>
<Customer>
<Code>Code0</Code>
<Name>Name0</Name>
</Customer>
<Customer>
<Code>Code1</Code>
<Name>Name1</Name>
</Customer>
<Customer>
<Code>Code2</Code>
<Name>Name2</Name>
</Customer>
</nRoot>
//XmlElement生成XML
XmlDocument xmleDoc = new XmlDocument();
XmlElement xmleRoot = xmleDoc.CreateElement("eRoot");
xmleDoc.AppendChild(xmleRoot);
for (int i = 0; i < 3; i++)
{
XmlElement xmlEle = xmleDoc.CreateElement("Customer");
xmlEle.SetAttribute("Code","Code"+i.ToString());
xmlEle.SetAttribute("Name", "Name" + i.ToString());
xmlEle.InnerText = "Code" + i.ToString();
xmleRoot.AppendChild(xmlEle);
}
生成的结果为:
<eRoot>
<Customer Code="Code0" Name="Name0">Code0</Customer>
<Customer Code="Code1" Name="Name1">Code1</Customer>
<Customer Code="Code2" Name="Name2">Code2</Customer>
</eRoot>