2016-12-13 20 views
0

xmlファイルをオブジェクト型に変換し、そのオブジェクトをWebサービスに渡す必要があります。 サンプルxmlをPFBします。XMLファイルをオブジェクト型に変換する際の問題

Sample XML

deserializeこのXMLながら、私は以下の例外を取得しています。

**`System.InvalidOperationException**` 
Additional information: There was an error generating the XML document. 
Message: The type `CallingWebserviceTest.Claims` was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. 

サンプルコードは:

XmlDocument doc = new XmlDocument(); 
doc.Load(Path) 
StringWriter sw = new StringWriter(); 
XmlTextWriter tx = new XmlTextWriter(sw); 
doc.WriteTo(tx); 
Claims c = new Claims(); 
StringReader strReader = new StringReader(sw.ToString()); 
XmlSerializer serializer = new XmlSerializer(typeof(Claims)); 
XmlTextReader xmlReader = new XmlTextReader(strReader); 
c = (Claims) serializer.Deserialize(xmlReader); 

私も例外を処理するために、XmlIncludeを追加しましたが、その後も、それを取得しています。クレームクラスの

サンプルコード:

[XmlInclude(typeof(CallingWebserviceTest.Claims))] 
[Serializable] 
//[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public class Claims 
{ 
     --- 
} 

[XmlInclude(typeof(CallingWebserviceTest.ClaimsClaim))] 
[Serializable] 
//[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public class ClaimsClaim 
{ 
     --- 
     --- 
} 

誰も私がこの問題を解決する助けてくださいことはできますか? xmlをオブジェクト型に変換するための他の提案も歓迎します。

+0

を使用していますか?このようなもの: 'public class ClaimsClaim:Claims' – Nino

+0

[mcve]を提供し、あなたのXMLを画像ではなく*テキスト*として含めます。あなたはあなたが提供したもので問題を再現することはできません([this fiddle](https://dotnetfiddle.net/Zguf7E)参照)。 XMLの生成中にエラーが発生したように見えるため、関連性がないようです。 –

答えて

0

コードは、あなたのクラスでいくつかの継承を持っているん

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


namespace ConsoleApplication32 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      Claims claims = new Claims() 
      { 
       claims = doc.Descendants("Claim").Select(x => new ClaimsClaim() 
       { 
        claimID = (string)x.Element("ID"), 
        personID = (string)x.Element("Person").Element("ID"), 
        gender = (string)x.Element("Person").Element("Gender"), 
        encounterType = (int)x.Element("Encounter").Element("Type"), 
        codeTerm = (string)x.Element("Diagnosis").Element("CodeTerm"), 
        codeType = (string)x.Element("Diagnosis").Element("Type"), 
        code = (string)x.Element("Diagnosis").Element("Code"), 
        activityID = (string)x.Element("Activity").Element("ID"), 
        activityCodeTerm = (string)x.Element("Activity").Element("CodeTerm"), 
        start = (DateTime)x.Element("Activity").Element("Start"), 
        activityCode = (int)x.Element("Activity").Element("Code"), 
        quantity = (int)x.Element("Activity").Element("Quantity"), 
        clinician = (string)x.Element("Activity").Element("Clinician"), 
       }).ToList() 
      }; 

     } 
    } 

    public class Claims 
    { 
     public List<ClaimsClaim> claims { get; set; } 
    } 


    public class ClaimsClaim 
    { 
     public string claimID { get; set; } 
     public string personID { get; set; } 
     public string gender { get; set; } 
     public int encounterType { get; set; } 
     public string codeTerm { get; set; } 
     public string codeType { get; set; } 
     public string code { get; set; } 
     public string activityID { get; set; } 
     public string activityCodeTerm { get; set; } 
     public DateTime start { get; set; } 
     public int activityCode { get; set; } 
     public int quantity { get; set; } 
     public string clinician { get; set; } 
    } 
} 

結果を平らにし、XML LINQ

+0

このコードを使用した後に同じ例外が発生する –

+0

xmlを検証する必要があります。 VSを使用することができます。メニュー:プロジェクト:新規アイテムの追加:XmlファイルXMLをビューに貼り付けます。エラーはエラーリストウィンドウにコンパイラエラーのように表示されます。 – jdweng

関連する問題