0

这是我的 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."

我究竟做错了什么?我假设它与命名空间有关,我尝试将命名空间更改为“”,从“XmlRoot”属性中完全删除命名空间属性,并完全删除“XmlRoot”属性。但这些都不起作用。

4

3 回答 3

1

你可以删除

[XmlRoot(Namespace="",...

因为您的文件不包含namespace:xmlns不存在

于 2012-09-18T18:56:05.940 回答
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,然后添加额外的属性,直到一切正常或失败。我会逐课做这门课,看看它是否有效,或者哪个课失败了。

于 2012-09-18T19:01:08.353 回答
0

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

所以只需更换

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

 XmlSerializer serializer = new XmlSerializer(myObject.GetType(), 
                                                 new XmlRootAttribute("Surveys"));
于 2012-09-18T19:20:03.447 回答