XML 中的 C# 元素未绑定到类

Posted

技术标签:

【中文标题】XML 中的 C# 元素未绑定到类【英文标题】:C# Element from XML Not Getting Bound To Class 【发布时间】:2021-05-18 02:39:28 【问题描述】:

我有以下 XML -

My XML

类结构->

public class AvailabilityResponse
  
    public string property_id  get; set; 
    public List<AvailRoom> rooms  get; set; 
    public Links links  get; set; 
    public int score  get; set; 
    public string Type  get; set; 
    public string Message  get; set; 
    public string status  get; set; 
  
public class AvailRoom
  
    public string id  get; set; 
    public string room_name  get; set; 
    public List<AvailRate> rates  get; set; 
  
[Serializable]
  public class AvailRate
  
    public string id  get; set; 
    public int available_rooms  get; set; 
    public bool refundable  get; set; 
    public bool fenced_deal  get; set; 
    public bool fenced_deal_available  get; set; 
    public bool deposit_required  get; set; 
    public string merchant_of_record  get; set; 
    public List<Amenity> amenities  get; set; 
    public Links links  get; set; 
    //[XmlElement(ElementName = "bed_groups")]
    //public List<BedGroups> bed_groups  get; set; 
    [XmlElement(ElementName = "bed_groups")]
    public List<BedGroup> bed_groups  get; set; 
    public List<CancelPenalty> cancel_penalties  get; set; 
    public Dictionary<string, occupancy_pricing> occupancy_pricing  get; set; 
    public occupancy_pricing Occupancy  get; set; 
  
public class BedGroup
  
    public Links links  get; set; 
    public List<Configuration> configuration  get; set; 
  
[Serializable]
  public class Links
  

    public PaymentOptions payment_options  get; set; 

    public Book book  get; set; 

    public PriceCheck price_check  get; set; 

    public AdditionalRates additional_rates  get; set; 

    public Retrieve retrieve  get; set; 

    public Cancel cancel  get; set; 

    public Change change  get; set; 

    public string method  get; set; 

    public string href  get; set; 

  
[Serializable]
  public class PriceCheck
  
    public string method  get; set; 
    public string href  get; set; 
  

结构的问题是,除了bed_groups,所有其他元素都被绑定了。

我尝试了不同的条件,比如用 Visual Studio 序列化 xml 类,用不同的标签注释它们。但这没有用。

例如。 cancel_penalties 具有类似的结构,但它正确绑定到 c# 类。 bed_groups 没有被绑定。每次它都为空,尽管值存在于 xml 中。

是因为 - &lt;a:item xmlns:a="item" item="37316" type="object"&gt; in bed_groups 和其他标签它只有 - &lt;item type="object"&gt; 吗?

有什么办法可以解决这个问题?

【问题讨论】:

【参考方案1】:

您需要命名空间。下面的代码是您的类的一个子集,并且已经过测试并且可以工作

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

namespace ConsoleApplication1

    class Program
    
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serizlizer = new XmlSerializer(typeof(AvailabilityResponse));
            AvailabilityResponse response = (AvailabilityResponse)serizlizer.Deserialize(reader);
        
    
    public class AvailabilityResponse
    
        [XmlArray("item")]
        [XmlArrayItem("rooms")]
        public List<AvailRoom> rooms  get; set; 
    
    public class AvailRoom
    
        public Item item  get; set; 
    
    public class Item
    
        public string id  get; set; 
        public string room_name  get; set; 
        [XmlArray("rates")]
        [XmlArrayItem("item")]
        public List<AvailRate> rates  get; set; 
    
    [Serializable]
    public class AvailRate
    
        [XmlElement(ElementName = "bed_groups")]
        public List<BedGroup> bed_groups  get; set; 
    
    public class BedGroup
    
        [XmlElement(ElementName = "item", Namespace = "item")]
        public BedGroupItem item  get; set; 
    
    public class BedGroupItem
    
        [XmlElement(Namespace = "")]
        public Links links  get; set; 
        [XmlElement(Namespace = "")]
        public List<Configuration> configuration  get; set; 
    
    public class Links
    
    
    public class Configuration
    
    
 

【讨论】:

【参考方案2】:

您需要使用下面描述的类结构。 `

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp

[XmlRoot(ElementName="property_id")]
public class Property_id 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="id")]
public class Id 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="room_name")]
public class Room_name 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="status")]
public class Status 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="available_rooms")]
public class Available_rooms 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="refundable")]
public class Refundable 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="member_deal_available")]
public class Member_deal_available 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="package")]
public class Package 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="member")]
public class Member 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="corporate")]
public class Corporate 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="distribution")]
public class Distribution 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="sale_scenario")]
public class Sale_scenario 
    [XmlElement(ElementName="package")]
    public Package Package  get; set; 
    [XmlElement(ElementName="member")]
    public Member Member  get; set; 
    [XmlElement(ElementName="corporate")]
    public Corporate Corporate  get; set; 
    [XmlElement(ElementName="distribution")]
    public Distribution Distribution  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="deposit_required")]
public class Deposit_required 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="merchant_of_record")]
public class Merchant_of_record 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="name")]
public class Name 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="item", Namespace="item")]
public class Item 
    [XmlElement(ElementName="id")]
    public Id Id  get; set; 
    [XmlElement(ElementName="name")]
    public Name Name  get; set; 
    [XmlAttribute(AttributeName="a", Namespace="http://www.w3.org/2000/xmlns/")]
    public string A  get; set; 
    [XmlAttribute(AttributeName="item")]
    public string _item  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlElement(ElementName="description")]
    public Description Description  get; set; 
    [XmlElement(ElementName="links")]
    public Links Links  get; set; 
    [XmlElement(ElementName="configuration")]
    public Configuration Configuration  get; set; 
    [XmlElement(ElementName="nightly")]
    public Nightly Nightly  get; set; 
    [XmlElement(ElementName="totals")]
    public Totals Totals  get; set; 


[XmlRoot(ElementName="amenities")]
public class Amenities 
    [XmlElement(ElementName="item", Namespace="item")]
    public List<Item> Item  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="method")]
public class Method 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="href")]
public class Href 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="payment_options")]
public class Payment_options 
    [XmlElement(ElementName="method")]
    public Method Method  get; set; 
    [XmlElement(ElementName="href")]
    public Href Href  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="links")]
public class Links 
    [XmlElement(ElementName="payment_options")]
    public Payment_options Payment_options  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlElement(ElementName="price_check")]
    public Price_check Price_check  get; set; 
    [XmlElement(ElementName="additional_rates")]
    public Additional_rates Additional_rates  get; set; 
    [XmlElement(ElementName="recommendations")]
    public Recommendations Recommendations  get; set; 


[XmlRoot(ElementName="description")]
public class Description 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="price_check")]
public class Price_check 
    [XmlElement(ElementName="method")]
    public Method Method  get; set; 
    [XmlElement(ElementName="href")]
    public Href Href  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="type")]
public class Type 
    [XmlAttribute(AttributeName="type")]
    public string _type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="size")]
public class Size 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="quantity")]
public class Quantity 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="item")]
public class Item2 
    [XmlElement(ElementName="type")]
    public Type Type  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string _Type  get; set; 
    [XmlElement(ElementName="size")]
    public Size Size  get; set; 
    [XmlElement(ElementName="quantity")]
    public Quantity Quantity  get; set; 
    [XmlElement(ElementName="item")]
    public List<Item2> Item2  get; set; 
    [XmlElement(ElementName="id")]
    public Id Id  get; set; 
    [XmlElement(ElementName="status")]
    public Status Status  get; set; 
    [XmlElement(ElementName="available_rooms")]
    public Available_rooms Available_rooms  get; set; 
    [XmlElement(ElementName="refundable")]
    public Refundable Refundable  get; set; 
    [XmlElement(ElementName="member_deal_available")]
    public Member_deal_available Member_deal_available  get; set; 
    [XmlElement(ElementName="sale_scenario")]
    public Sale_scenario Sale_scenario  get; set; 
    [XmlElement(ElementName="deposit_required")]
    public Deposit_required Deposit_required  get; set; 
    [XmlElement(ElementName="merchant_of_record")]
    public Merchant_of_record Merchant_of_record  get; set; 
    [XmlElement(ElementName="amenities")]
    public Amenities Amenities  get; set; 
    [XmlElement(ElementName="links")]
    public Links Links  get; set; 
    [XmlElement(ElementName="bed_groups")]
    public Bed_groups Bed_groups  get; set; 
    [XmlElement(ElementName="occupancy_pricing")]
    public Occupancy_pricing Occupancy_pricing  get; set; 
    [XmlElement(ElementName="promotions")]
    public Promotions Promotions  get; set; 
    [XmlElement(ElementName="room_name")]
    public Room_name Room_name  get; set; 
    [XmlElement(ElementName="rates")]
    public Rates Rates  get; set; 


[XmlRoot(ElementName="configuration")]
public class Configuration 
    [XmlElement(ElementName="item")]
    public Item2 Item2  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="bed_groups")]
public class Bed_groups 
    [XmlElement(ElementName="item", Namespace="item")]
    public Item Item  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="value")]
public class Value 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="currency")]
public class Currency 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="nightly")]
public class Nightly 
    [XmlElement(ElementName="item")]
    public Item2 Item2  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="request_currency")]
public class Request_currency 
    [XmlElement(ElementName="value")]
    public Value Value  get; set; 
    [XmlElement(ElementName="currency")]
    public Currency Currency  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="billable_currency")]
public class Billable_currency 
    [XmlElement(ElementName="value")]
    public Value Value  get; set; 
    [XmlElement(ElementName="currency")]
    public Currency Currency  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="gross_profit")]
public class Gross_profit 
    [XmlElement(ElementName="request_currency")]
    public Request_currency Request_currency  get; set; 
    [XmlElement(ElementName="billable_currency")]
    public Billable_currency Billable_currency  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="inclusive")]
public class Inclusive 
    [XmlElement(ElementName="request_currency")]
    public Request_currency Request_currency  get; set; 
    [XmlElement(ElementName="billable_currency")]
    public Billable_currency Billable_currency  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="exclusive")]
public class Exclusive 
    [XmlElement(ElementName="request_currency")]
    public Request_currency Request_currency  get; set; 
    [XmlElement(ElementName="billable_currency")]
    public Billable_currency Billable_currency  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="strikethrough")]
public class Strikethrough 
    [XmlElement(ElementName="request_currency")]
    public Request_currency Request_currency  get; set; 
    [XmlElement(ElementName="billable_currency")]
    public Billable_currency Billable_currency  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="marketing_fee")]
public class Marketing_fee 
    [XmlElement(ElementName="request_currency")]
    public Request_currency Request_currency  get; set; 
    [XmlElement(ElementName="billable_currency")]
    public Billable_currency Billable_currency  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="totals")]
public class Totals 
    [XmlElement(ElementName="gross_profit")]
    public Gross_profit Gross_profit  get; set; 
    [XmlElement(ElementName="inclusive")]
    public Inclusive Inclusive  get; set; 
    [XmlElement(ElementName="exclusive")]
    public Exclusive Exclusive  get; set; 
    [XmlElement(ElementName="strikethrough")]
    public Strikethrough Strikethrough  get; set; 
    [XmlElement(ElementName="marketing_fee")]
    public Marketing_fee Marketing_fee  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="occupancy_pricing")]
public class Occupancy_pricing 
    [XmlElement(ElementName="item", Namespace="item")]
    public Item Item  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="deal")]
public class Deal 
    [XmlElement(ElementName="id")]
    public Id Id  get; set; 
    [XmlElement(ElementName="description")]
    public Description Description  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="promotions")]
public class Promotions 
    [XmlElement(ElementName="deal")]
    public Deal Deal  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="rates")]
public class Rates 
    [XmlElement(ElementName="item")]
    public Item Item  get; set; 
    [XmlElement(ElementName="item")]
    public Item2 Item2  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="rooms")]
public class Rooms 
    [XmlElement(ElementName="item")]
    public Item Item  get; set; 
    [XmlElement(ElementName="item")]
    public List<Item2> Item2  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="additional_rates")]
public class Additional_rates 
    [XmlElement(ElementName="method")]
    public Method Method  get; set; 
    [XmlElement(ElementName="href")]
    public Href Href  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="recommendations")]
public class Recommendations 
    [XmlElement(ElementName="method")]
    public Method Method  get; set; 
    [XmlElement(ElementName="href")]
    public Href Href  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 


[XmlRoot(ElementName="score")]
public class Score 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
    [XmlText]
    public string Text  get; set; 


[XmlRoot(ElementName="AvailabilityResponse")]
public class AvailabilityResponse 
    [XmlElement(ElementName="item")]
    public Item Item  get; set; 
    [XmlAttribute(AttributeName="type")]
    public string Type  get; set; 
`

您可以将您的 XML 粘贴到 XMLTOC# 以将任何 XML 转换为 c# 类

【讨论】:

我试过这个。为什么我的班级结构中的 cancel_penalties 工作正常而不是 bed_groups ?

以上是关于XML 中的 C# 元素未绑定到类的主要内容,如果未能解决你的问题,请参考以下文章

如何修复解析 XML:android 中的未绑定前缀

Kivy,Python3.5 - 将用户文本输入绑定到类中的方法

使用 Blazor 将输入文本动态绑定到类/对象属性

Flex 警告:无法绑定到类“Object”上的属性“foo”(类不是 IEventDispatcher)

xml文件中的错误(android编程):未绑定前缀[重复]

如何在未绑定的 xml 结构中生成逗号分隔的字符串