我更喜欢用于 XML 创建的 XML Linq 类。书写和阅读要容易得多。
您可以将其与“使用 System.Xml.Linq;”绑定。
现在您可以使用“new XAttribute”在元素中创建一个属性。
这是一个小例子:
//build base
XNamespace myNs = "http://www.w3.org/2001/XMLSchema-instance";
XDocument myDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("newDocument",
new XAttribute(XNamespace.Xmlns + "xsi", myNs),
new XAttribute(myNs + "noNamespaceSchemaLocation", "ArchiveDocument.xsd"),
new XElement("document",
new XAttribute("instanceDate", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")),
new XElement("attribute",
new XAttribute("attributeDefinitionId", "Belegart"),
new XElement("value", reportType)),
new XElement("attribute",
new XAttribute("attributeDefinitionId", "Date"),
new XElement("value", Date.ToString("yyyyMMdd"))),
new XElement("attribute",
new XAttribute("attributeDefinitionId", "Kalenderjahr"),
new XElement("value", Date.ToString("yyyy"))),
new XElement("attribute",
new XAttribute("attributeDefinitionId", "Kalendermonat"),
new XElement("value", Date.Month.ToString())),
new XElement("attribute",
new XAttribute("attributeDefinitionId", "Mitglieds_Nummer"),
new XElement("value", partnerId.ToString())))));