2017-10-06 72 views
0

XmlDocument的价值属性产生像下面XMLDOCUMENT显示值作为XML

<type>document</type> 

我希望它产生这样

<type value = "document"></type> 

有一个简单的方法来做到这一点对我来说所有入境?

只是为了添加更多的细节我有一个json,我从中使用JsonConvert.DeserializeXmlNode将其转换为xml。

当我转换使用此API,我得到的值这样的 -

<type>document</type> 

我希望它是 -

<type value = "document"></type> 
+0

你是如何产生的?你是否在POCO对象上使用注解(XmlAttribute,XmlElement)? – brugnner

+0

不,我不使用annotaion其在POCO中的简单属性,如下所示 - public BundleType Type { get;组; }其中Bundletype值是文档 – ankush

回答

0

下面是示例代码来生成属性。

XmlDocument doc = new XmlDocument(); 
XmlNode node = doc.CreateNode(XmlNodeType.Element, "type", ""); 
XmlAttribute attr = doc.CreateAttribute("value"); 
attr.Value = "Document"; 
node.Attributes.Append(attr); 
doc.AppendChild(node); 
var outputString = doc.InnerXml; 
0

我喜欢使用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())))));