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

あなたの問題は名前空間とは関係ありません。あなたのxmlのルートタグはSurveys

だから交換するだけ

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

 XmlSerializer serializer = new XmlSerializer(myObject.GetType(), 
                                                 new XmlRootAttribute("Surveys"));
于 2012-09-18T19:20:03.447 に答える
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 に答える