Amanda Kitson
Amanda Kitson

Reputation: 5547

Cannot deserialize XML object

Here is my XML string:

 <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>

Here are my classes..

Survey:

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; }
    }
}

Erosion:

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; }
    }
}

Span:

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; }
    }
}

And here is the method I am using to deserialize the 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;
        }

And here is the code where I call it:

                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;
                    }
                }

When I attempt to execute this, I get the following error:

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

What am I doing wrong? I assume it is something with the namespacing, and I have tried changing the namespace to "", removing the namespace attribute completely from the "XmlRoot" attribute, and also completely removing the "XmlRoot" attribute. But none of that is working.

Upvotes: 0

Views: 2083

Answers (3)

L.B
L.B

Reputation: 116098

Your problem is not related with namespaces. The root tag in your xml is Surveys

So just replace

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

with

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

Upvotes: 0

Robert H
Robert H

Reputation: 11730

Are you able to pass in a namespace via:

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

perhaps this will also work:

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

I have always used this to serialize my class to string, not deserialize it back to an object so YMMV.

--Edit --

Can you try:

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

See if that will deserialize back properly. If it does start adding your L,W,H and then tack on extra attributes until it all works, or fails. I would do this class by class to see if it works, or which class is failing.

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can delete

[XmlRoot(Namespace="",...

Because your file does not contain namespace : xmlns does not exist

Upvotes: 1

Related Questions