XML Desialization问题

时间:2016-06-12 08:47:38

标签: c# xml xmlserializer

我正在尝试反序列化xml但有一些问题。

我的XML是,

<?xml version="1.0" encoding="UTF-8" ?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <WTR_BagsCreateRS xmlns:common="http://sita.aero/wtr/common/7/0" 
        xmlns:iata="http://www.iata.org/IATA/2007/00" xmlns="http://sita.aero/WTR_BagsCreateRS/7/0">
          <Success /> 
        <RecordID>
          <common:RecordType>DELAYED</common:RecordType> 
          <common:RecordReference AirlineCode="QR" ReferenceNumber="10963" StationCode="DOH" /> 
        </RecordID>
        </WTR_BagsCreateRS>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我想要反序列化XML的类型,

 [Serializable]
    [DesignerCategory("code")]
    [DebuggerStepThrough]
    [XmlType(AnonymousType = true, Namespace = "http://sita.aero/WTR_BagsCreateRS/7/0")]
    [GeneratedCode("System.Xml", "4.0.30319.34283")]
    [XmlRoot(IsNullable = false, Namespace = "http://www.sita.aero/WTR_BagsCreateRS/7/0")]
    public class WTR_BagsCreateRS : INotifyPropertyChanged
    {
        public WTR_BagsCreateRS();

        [XmlElement("RecordID", typeof(RecordIdentifierType), Order = 0)]
        [XmlElement("Errors", typeof(ErrorsType), Order = 0)]
        [XmlElement("Warnings", typeof(WarningsType), Order = 0)]
        [XmlElement("Success", typeof(SuccessType), Order = 0)]
        public object[] Items { get; set; }
        [XmlAttribute]
        public WTR_LostPropertyRegisterRQModuleID ModuleID { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName);
    }

我的脱离代码如下,

public static T DeserializeObject<T>(string xmlString)
        {
            T result;

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlString);
            XmlSerializer serializer = new XmlSerializer(typeof(T));//, new XmlRootAttribute("http://sita.aero/WTR_BagsCreateRS/7/0"));
            var body = doc.GetElementsByTagName("SOAP-ENV:Body")[0];
            using (TextReader reader = new StringReader(body.InnerXml))
            {
                result = (T)serializer.Deserialize(reader);
            }

            return result;
        }

反序列化时我面临以下问题, <WTR_BagsCreateRS xmlns='http://sita.aero/WTR_BagsCreateRS/7/0'> was not expected.

1 个答案:

答案 0 :(得分:1)

在您提供的课程定义中

[XmlRoot(IsNullable = false, Namespace = "http://www.sita.aero/WTR_BagsCreateRS/7/0")]

在XML中你有

xmlns="http://sita.aero/WTR_BagsCreateRS/7/0"

删除www.

,更改命名空间