我喜欢使用XML的LINQ类的XML创作。写和读更容易。 您可以将它与“使用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())))));
你是如何产生的?你是否在POCO对象上使用注解(XmlAttribute,XmlElement)? – brugnner
不,我不使用annotaion其在POCO中的简单属性,如下所示 - public BundleType Type { get;组; }其中Bundletype值是文档 – ankush