0

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

}

1
  • Change RPSDocuments : [XmlRoot(ElementName = "Documents", Namespace = "mycompany.com/api/v2")] Commented Jun 6, 2017 at 15:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rafal - That was a huge help!! I couldn't figure out what to do with that type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.