使用包含C#的元素反序列化xml,该元素具有不同命名空间中的属性

时间:2017-06-06 15:33:04

标签: c# xml xml-deserialization

我在一个我无法控制的结构中接收XML,并且正在尝试使用C#反序列化XML。 XML包含多个名称空间。大多数XML都在1个命名空间中,但有一部分在不同的命名空间中有一个属性。我的问题是Content节点始终被反序列化为null。我需要做些什么才能纠正这个问题?

我的XML在下面给出。

<Documents xmlns="http://mycompany.com/api/v2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Document>
       <Id>100000</Id>
       <Title>Document Title</Title>
       <Locale>en-US</Locale>
       <Status>Archived</Status>
       <PublishDate>2016-06-01T16:40:00</PublishDate>
       <PublishDateUTC>2016-06-01T21:40:00Z</PublishDateUTC>
       <UpdateDateUTC>2016-06-01T21:40:00Z</UpdateDateUTC>
       <Companies>
           <Company>
               <Id>1C000TX2343</Id>
               <Name>Company Name</Name>
               <Status>Public</Status>
               <OperationStatus>N</OperationStatus>
               <Country>USA</Country>
           </Company>
       </Companies>
       <Content i:type="CRCMinute">
           <AssetClass>Corporate</AssetClass>
           <CommitteeAlphaRating>BB+</CommitteeAlphaRating>
           <CommitteeCreditTrend>Negative</CommitteeCreditTrend>
           <CommitteeMeetingDate>2016-06-01T00:00:00</CommitteeMeetingDate>
           <CreditWatch i:nil="true"/>
           <RatingStatus i:nil="true"/>
           <ShortTermRating i:nil="true"/>
           <SignatureDate>2016-06-01T16:40:00</SignatureDate>
           <SignatureText>Alfred Neumann</SignatureText>
       </Content>
    </Document>
</Documents>

我还提供了用于反序列化XML的类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace Morningstar.CreditRatings.CorporateShortTerm
{
[XmlRoot("Documents")]
public class RPSDocuments
{
    [XmlElement("Document")]
    public List<DocumentData> DocumentData { get; set; }
}

[Serializable]
public class DocumentData
{
    [XmlElement]
    public string Id { get; set; }

    [XmlElement]
    public string Title { get; set; }

    [XmlElement]
    public string PublishDate { get; set; }

    [XmlElement]
    public string PublishDateUTC { get; set; }

    [XmlElement]
    public string UpdateDateUTC { get; set; }

    [XmlArray("Companies")]
    [XmlArrayItem("Company")]
    public List<CompanyData> Companies { get; set; }

    [XmlElement("Content", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public DocumentContentData Content { get; set; }
}

[Serializable]
public class CompanyData
{
    [XmlElement]
    public string Id { get; set; }

    public string Name { get; set; }

    [XmlElement]
    public string Status { get; set; }

    [XmlElement]
    public string OperationStatus { get; set; }

    [XmlElement]
    public string Country { get; set; }
}

[Serializable]
public class DocumentContentData
{
    [XmlElement]
    public string Analyst { get; set; }

    [XmlElement]
    public string AssetClass { get; set; }

    [XmlElement]
    public string CommitteeAlphaRating { get; set; }

    [XmlElement]
    public string CommitteeCreditTrend { get; set; }

    [XmlElement]
    public string CommitteeMeetingDate { get; set; }

    [XmlElement]
    public string CreditWatch { get; set; }

    [XmlElement]
    public string RatingStatus { get; set; }

    [XmlElement]
    public string ShortTermRating { get; set; }

    [XmlElement]
    public string SignatureDate { get; set; }

    [XmlElement]
    public string SignatureText { get; set; }
}

}

1 个答案:

答案 0 :(得分:1)

由于Content元素本身与其父元素不在不同的命名空间(仅type属性),因此从Namespace属性中删除Content属性。相反,您必须使用

装饰RPSDocuments
[Namespace = "http://mycompany.com/api/v2"]

编辑:您还需要使用

装饰DocumentContentData
[XmlType("CRCMinute")]

您不需要Serializable属性。