无法反序列化XML对象

时间:2012-09-18 18:53:50

标签: c# xml xml-deserialization

这是我的XML字符串:

 <Surveys>
   <Survey>
      <SurveyID>33</SurveyID>
      <CulvertID>5</CulvertID>
      <PRNo>599422</PRNo>
      <MP>5.243</MP>
      <RefMiles>1.500</RefMiles>
      <PtID>ABCGEFGH</PtID>
      <FWVersion>10</FWVersion>
      <SurveyDate>2012-08-21</SurveyDate>
      <FutureComments>a33537e2-6c2a-4bde-ac99-4da82a93c598</FutureComments>
      <Erosions>
         <Erosion>
            <ID>158</ID>
            <SurveyID>33</SurveyID>
            <Location>Test Erosion - 8/21/2012 - 10:16:10 AM</Location>
         </Erosion>
         <Erosion>
            <ID>159</ID>
            <SurveyID>33</SurveyID>
            <Location>Test Erosion - 8/21/2012 - 10:16:10 AM</Location>
         </Erosion>
      </Erosions>
      <Spans>
         <Span>
            <ID>86</ID>
            <SurveyID>33</SurveyID>
            <Material>Test Span - 8/21/2012 - 10:16:10 AM</Material>
         </Span>
         <Span>
            <ID>87</ID>
            <SurveyID>33</SurveyID>
            <Material>Test Span - 8/21/2012 - 10:16:10 AM</Material>
         </Span>
      </Spans>
   </Survey>
   <Survey>
      <SurveyID>35</SurveyID>
      <CulvertID>5</CulvertID>
      <PRNo>587180</PRNo>
      <MP>5.243</MP>
      <RefMiles>1.500</RefMiles>
      <PtID>ABCGEFGH</PtID>
      <FWVersion>10</FWVersion>
      <SurveyDate>2012-08-27</SurveyDate>
      <FutureComments>7c36fe43-78cb-436e-81cf-f416aa63c8fc</FutureComments>
      <Erosions>
         <Erosion>
            <ID>160</ID>
            <SurveyID>35</SurveyID>
            <Location>Test Erosion - 8/27/2012 - 1:19:04 PM</Location>
         </Erosion>
         <Erosion>
            <ID>161</ID>
            <SurveyID>35</SurveyID>
            <Location>Test Erosion - 8/27/2012 - 1:19:04 PM</Location>
         </Erosion>
      </Erosions>
      <Spans>
         <Span>
            <ID>88</ID>
            <SurveyID>35</SurveyID>
            <Material>Test Span - 8/27/2012 - 1:19:04 PM</Material>
         </Span>
         <Span>
            <ID>89</ID>
            <SurveyID>35</SurveyID>
            <Material>Test Span - 8/27/2012 - 1:19:04 PM</Material>
         </Span>
      </Spans>
   </Survey>
</Surveys>

这是我的课程..

调查:

namespace DNR.CulvertInventory.SERVICE
{
    [XmlRoot(Namespace="",
     ElementName = "Survey",
     DataType = "string",
     IsNullable = true)]
    public class Survey: BOBase
    {
        public int SurveyID { get; set; }
        public int CulvertID { get; set; }
        public string County { get; set; }
        public string RoadName { get; set; }
        public int PRNo { get; set; }
        public decimal MP { get; set; }
        public decimal RefMiles { get; set; }
        public string PtID { get; set; }
        public short FWVersion { get; set; }
        public double? Latitude { get; set; }
        public double? Longitude { get; set; }
        public string Waterway { get; set; }
        public string SiteID { get; set; }
        public string Observers { get; set; }
        public DateTime SurveyDate { get; set; }
        public string AdjacentLandowner { get; set; }
        public string LocationComments { get; set; }
        public short? CrossingType { get; set; }
        public string StructureShape { get; set; }
        public string InletStructure { get; set; }
        public string OutletStructure { get; set; }
        public string OutletType { get; set; }
        public string SubstrateInStructure { get; set; }
        public string GeneralCondition { get; set; }
        public short? PluggedPercent { get; set; }
        public string PluggedLocation { get; set; }
        public short? CrushedPercent { get; set; }
        public string CrushedLocation { get; set; }
        public bool? IsRustedThrough { get; set; }
        public string StructureInterior { get; set; }
        public decimal? StructureWaterDepthInlet { get; set; }
        public decimal? StructureWaterDepthOutlet { get; set; }
        public decimal? StructureEmbeddedDepthInlet { get; set; }
        public decimal? StructureEmbeddedDepthOutlet { get; set; }
        public decimal? StructureWaterVelocityInlet { get; set; }
        public decimal? StructureWaterVelocityOutlet { get; set; }
        public decimal? StructureWaterVelocityMeasured { get; set; }
        public string StructureWaterVelocityMeasuredWith { get; set; }
        public bool? IsPerched { get; set; }
        public decimal? PerchHeight { get; set; }
        public string StreamFlow { get; set; }
        public bool? IsScourPoolPresent { get; set; }
        public decimal? ScourPoolLength { get; set; }
        public decimal? ScourPoolWidth { get; set; }
        public decimal? ScourPoolDepth { get; set; }
        public bool? IsUpstreamPondPresent { get; set; }
        public decimal? UpstreamPondLength { get; set; }
        public decimal? UpstreamPondWidth { get; set; }
        public decimal? RiffleWaterDepth { get; set; }
        public decimal? RiffleBankfullWidth { get; set; }
        public decimal? RiffleWettedWidth { get; set; }
        public decimal? RiffleWaterVelocity { get; set; }
        public string RiffleMeasuredWith { get; set; }
        public string RiffleSubstrate { get; set; }
        public string RoadSurface { get; set; }
        public string RoadCondition { get; set; }
        public decimal? RoadWidth { get; set; }
        public string LocationOfLowPoint { get; set; }
        public string RunOffPath { get; set; }
        public decimal? FillDepthUpStream { get; set; }
        public decimal? FillDepthDownStream { get; set; }
        public string SlopeUpStream { get; set; }
        public string SlopeDownStream { get; set; }
        public decimal? ApproachLengthLeft { get; set; }
        public decimal? ApproachLengthRight { get; set; }
        public string ApproachSlopeLeft { get; set; }
        public string ApproachSlopeRight { get; set; }
        public string VegetationDitchLeft { get; set; }
        public string VegetationDitchRight { get; set; }
        public bool? IsErosionPresent { get; set; }
        public bool? IsErosionCorrectable { get; set; }
        public string ErosionExtent { get; set; }
        public string ErosionNotes { get; set; }
        public bool? IsPrioritySite { get; set; }
        public string PriorityReason { get; set; }
        public string PriorityComments { get; set; }
        public bool? FutureVisit { get; set; }
        public string FutureComments { get; set; }
        public bool? NonNativeInvasiveSpecies { get; set; }
        public string SpeciesObserved { get; set; }
        public bool? IsHeadChanged { get; set; }
        public string HeadChangeComments { get; set; }
        public bool? IsBackwatered { get; set; }
        public string BackwaterComments { get; set; }
        public bool? IsOvertopping { get; set; }
        public string OvertoppingComments { get; set; }
        public bool? IsSubstrateEntireLength { get; set; }
        public decimal? CalculatedPassability { get; set; }
        public decimal? DefinedPassability { get; set; }
        public string PassabilityComments { get; set; }
        public byte[] PhotoInlet { get; set; }
        public byte[] PhotoOutlet { get; set; }
        public byte[] PhotoUpstream { get; set; }
        public byte[] PhotoDownstream { get; set; }
        public byte[] PhotoRoadApproachLeft { get; set; }
        public byte[] PhotoRoadApproachRight { get; set; }
        public List<Erosion> Erosions { get; set; }
        public List<Span> Spans { get; set; }
    }
}

侵蚀:

namespace DNR.CulvertInventory.SERVICE
{
    [XmlRoot(Namespace = "",
     ElementName = "Erosion",
     DataType = "string",
     IsNullable = true)]
    public class Erosion : BOBase
    {
        public int ID { get; set; }
        public int SurveyID { get; set; }
        public string Location { get; set; }
        public decimal? Length { get; set; }
        public decimal? Width { get; set; }
        public decimal? Depth { get; set; }
        public bool? IsReachingStream { get; set; }
        public string MaterialEroded { get; set; }
    }
}

跨度:

namespace DNR.CulvertInventory.SERVICE
{
    [XmlRoot(Namespace = "",
     ElementName = "Span",
     DataType = "string",
     IsNullable = true)]
    public class Span
    {
        public int ID { get; set; }
        public int SurveyID { get; set; }
        public short? SpanNumber { get; set; }
        public decimal? Length { get; set; }
        public decimal? Width { get; set; }
        public decimal? Height { get; set; }
        public string Material { get; set; }
    }
}

这是我用来反序列化xml的方法:

public static T GetObjectFromXMLString<T>(string xml) where T: new()
        {
            T myObject = new T();

            XmlSerializer serializer = new XmlSerializer(myObject.GetType());

            StringReader stream = new StringReader(xml); // read xml data
            XmlTextReader reader = new XmlTextReader(stream);  // create reader

            object deserialized = serializer.Deserialize(reader);

            myObject = (T)deserialized;

            return myObject;
        }

以下是我称之为的代码:

                command.Connection.Open();
                using (XmlReader reader = command.ExecuteXmlReader())
                {
                    while (reader.Read())
                    {
                        string s = reader.ReadOuterXml();
                        // do something with s
                        List<T> myList = Helpers.GetObjectFromXMLString<List<T>>(s);
                        BOList = myList;
                    }
                }

当我尝试执行此操作时,出现以下错误:

"<Surveys xmlns=''> was not expected."

我做错了什么?我认为它与命名空间有关,我尝试将命名空间更改为&#34;&#34;,从&#34; XmlRoot&#34;中完全删除命名空间属性。属性,还完全删除&#34; XmlRoot&#34;属性。但这些都不起作用。

2 个答案:

答案 0 :(得分:0)

您是否可以通过以下方式传入命名空间:

var ns = new XmlSerializerNamespaces();
ns.Add("", "");

也许这也会奏效:

var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Encoding = new UnicodeEncoding(false, false) };

我一直用这个来将我的类序列化为字符串,而不是将它反序列化为一个对象,所以YMMV。

- 编辑 -

你可以尝试:

namespace DNR.CulvertInventory.SERVICE
{
    [XmlRoot("Span")]
    public class Span
    {
        public int ID { get; set; }
        public int SurveyID { get; set; }
        public string Material { get; set; }
    }
}

看看是否会正确反序列化。如果它确实开始添加你的L,W,H然后添加额外的属性,直到它全部工作或失败。我会按类来做这个类,看它是否有效,或哪个类失败。

答案 1 :(得分:0)

您的问题与命名空间无关。 xml中的根标记是Surveys

所以只需替换

 XmlSerializer serializer = new XmlSerializer(myObject.GetType());

 XmlSerializer serializer = new XmlSerializer(myObject.GetType(), 
                                                 new XmlRootAttribute("Surveys"));