简体   繁体   English

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

[英]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#. 我正在以无法控制的结构接收XML,并尝试使用C#对XML进行反序列化。 The XML contains multiple namespaces. XML包含多个名称空间。 Most of the XML is in 1 namespace, but there is a portion that has an attribute in a different namespace. 大多数XML都在1个命名空间中,但是有一部分具有不同命名空间中的属性。 My issue is that the Content node is always being deserialized as null. 我的问题是Content节点总是反序列化为null。 What do I need to do to correct this? 我需要做些什么来纠正这个问题?

My XML is given below. 我的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>

The class I am using to deserialize the XML is provided as well. 还提供了我用来反序列化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; }
}

} }

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. 由于Content元素本身与其父元素不在不同的名称空间中(仅type属性位于此Namespace ),因此请从Content属性中删除Namespace属性。 Instead you must decorate RPSDocuments with 相反,您必须使用以下命令装饰RPSDocuments

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

Edit: You also need to decorate DocumentContentData with 编辑:您还需要装饰DocumentContentData

[XmlType("CRCMinute")]

You don't need the Serializable attributes. 您不需要Serializable属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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