将XML文件转换为对象类型的问题

时间:2016-12-13 13:30:19

标签: c# xml object

我需要将xml文件转换为对象类型,并将该对象传递给Web服务。 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转换为对象类型的任何其他建议。

1 个答案:

答案 0 :(得分: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