【问题标题】:XML Desialization issueXML反序列化问题
【发布时间】:2016-06-12 08:47:38
【问题描述】:

我正在尝试反序列化一个 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;
        }

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

【问题讨论】:

    标签: c# xml xmlserializer


    【解决方案1】:

    在您提供的类定义中

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

    在 XML 中你有

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

    通过删除www.来更改命名空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-27
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 2018-10-26
      • 1970-01-01
      • 2014-10-04
      相关资源
      最近更新 更多