2017-10-06 5 views
0

XmlDocumentオブジェクトは、私のためにすべてのエントリを生成し、これを行う簡単な方法はありますか?のXmlDocumentショーの値は

詳細を追加するには、私はJsonConvert.DeserializeXmlNodeを使用してxmlに変換しているjsonを持っています。

私はこのAPIを使用して変換するときに、私はこのような値を取得する -

<type>document</type> 

私はそれになりたい -

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

はどのようにあなたはこれを生成していますか? POCOオブジェクトに対してアノテーション(XmlAttribute、XmlElement)を使用していますか? – brugnner

+0

いいえ私は以下のようにPOCOのannotaionを使っていません。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作成のためのXML Linqクラスを推奨します。書き込みや読み込みがはるかに簡単です。 "using 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())))));