简体   繁体   中英

Deserialize xml using C# containing element with an attribute in a different namespace

I am receiving XML in a structure I have no control over and am attempting to deserialize the XML using C#. The XML contains multiple namespaces. Most of the XML is in 1 namespace, but there is a portion that has an attribute in a different namespace. My issue is that the Content node is always being deserialized as null. What do I need to do to correct this?

My XML is given below.

<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>

The class I am using to deserialize the XML is provided as well.

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; }
}

}

Since the Content element itself is not in a different namespace than its parent (only the type attribute is), remove the Namespace attribute from the Content property. Instead you must decorate RPSDocuments with

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

Edit: You also need to decorate DocumentContentData with

[XmlType("CRCMinute")]

You don't need the Serializable attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM