2017-12-28 8 views
0

source xmlファイルがjava object xmlどのようにC#または任意のツールを使用してXMLファイルにJavaオブジェクトのXMLファイルを変換する?

input file 

<?xml version="1.0" encoding="utf-16" standalone="yes"?> 
<object class="MyMessage"> 
    <object class="RequestMessage"> 
    <field name="language"> 
     <null/> 
    </field> 
<field name="messageId"> 
     <value class="java.lang.String">85036585</value> 
    </field>  
    </object> 
</object> 

である私は、C#を使用して、通常のXML構造にすべての構造の上に変換したいまたは実行する任意のツールはありますか?

期待されるXML出力 -

<?xml version="1.0" encoding="utf-16" standalone="yes"?> 
    <MyMessage> 
     <RequestMessage> 
     <language> 
     </language> 
    <messageId> 
      85036585 
     </messageId>  
     </RequestMessage> 
    </MyMessage> 

答えて

1

私は以下のコードは、すべてのケースで動作するわからないんだけど、それはあなたの投稿をXMLで動作ありません。私はXMLのlinqを使用して再帰アルゴリズムを使用しています:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      StreamReader reader = new StreamReader(FILENAME, Encoding.Unicode); 
      reader.ReadLine(); //skip xml identification due to unicode not being recognized. 
      XDocument doc = XDocument.Load(reader); 

      XElement root = doc.Root; 

      string xmlHeader = string.Format("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><{0}></{0}>", (string)root.Attributes().FirstOrDefault()); 

      XDocument newDoc = XDocument.Parse(xmlHeader); 
      XElement newRoot = newDoc.Root; 
      RecursiveParse(root, newRoot); 
     } 
     static void RecursiveParse(XElement parent, XElement newParent) 
     { 
      List<XElement> children = parent.Elements().ToList(); 
      if (children != null) 
      { 
       foreach (XElement child in children) 
       { 

        if (child.Name.LocalName != "null") 
        { 
         string innerTag = (string)(XElement)child.FirstNode; 
         XElement newChild = new XElement((string)child.Attributes().FirstOrDefault()); 
         newParent.Add(newChild); 

         if (innerTag != "") 
         { 
          newParent.Add(innerTag); 
         } 
         else 
         { 
          RecursiveParse(child, newChild); 
         } 
        } 
       } 
      } 

     } 
    } 
} 
+0

追加情報:ルート要素がありません。 – Neo

+0

あなたは何をルート要素と呼んでいますか?私はすべてのタグを取得しています。 – jdweng

+0

プログラムの実行中にこのエラーが発生します。 – Neo

関連する問題