<tfoot id='iD0dU'></tfoot>

<i id='iD0dU'><tr id='iD0dU'><dt id='iD0dU'><q id='iD0dU'><span id='iD0dU'><b id='iD0dU'><form id='iD0dU'><ins id='iD0dU'></ins><ul id='iD0dU'></ul><sub id='iD0dU'></sub></form><legend id='iD0dU'></legend><bdo id='iD0dU'><pre id='iD0dU'><center id='iD0dU'></center></pre></bdo></b><th id='iD0dU'></th></span></q></dt></tr></i><div id='iD0dU'><tfoot id='iD0dU'></tfoot><dl id='iD0dU'><fieldset id='iD0dU'></fieldset></dl></div>

      1. <small id='iD0dU'></small><noframes id='iD0dU'>

        • <bdo id='iD0dU'></bdo><ul id='iD0dU'></ul>
        <legend id='iD0dU'><style id='iD0dU'><dir id='iD0dU'><q id='iD0dU'></q></dir></style></legend>

        如何在 C# 中使用属性列表反序列化元素

        时间:2023-07-27

      2. <legend id='YDBvZ'><style id='YDBvZ'><dir id='YDBvZ'><q id='YDBvZ'></q></dir></style></legend>

        <small id='YDBvZ'></small><noframes id='YDBvZ'>

              <tbody id='YDBvZ'></tbody>

                  <bdo id='YDBvZ'></bdo><ul id='YDBvZ'></ul>
                  <tfoot id='YDBvZ'></tfoot>
                  <i id='YDBvZ'><tr id='YDBvZ'><dt id='YDBvZ'><q id='YDBvZ'><span id='YDBvZ'><b id='YDBvZ'><form id='YDBvZ'><ins id='YDBvZ'></ins><ul id='YDBvZ'></ul><sub id='YDBvZ'></sub></form><legend id='YDBvZ'></legend><bdo id='YDBvZ'><pre id='YDBvZ'><center id='YDBvZ'></center></pre></bdo></b><th id='YDBvZ'></th></span></q></dt></tr></i><div id='YDBvZ'><tfoot id='YDBvZ'></tfoot><dl id='YDBvZ'><fieldset id='YDBvZ'></fieldset></dl></div>

                  本文介绍了如何在 C# 中使用属性列表反序列化元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我有以下 Xml 需要反序列化:

                  Hi I have the following Xml to deserialize:

                  <RootNode>
                      <Item
                        Name="Bill"
                        Age="34"
                        Job="Lorry Driver"
                        Married="Yes" />
                      <Item
                        FavouriteColour="Blue"
                        Age="12"
                      <Item
                        Job="Librarian"
                         />
                      </RootNote>
                  

                  当我不知道键名或会有多少属性时,如何使用属性键值对列表反序列化 Item 元素?

                  How can I deserialize the Item element with a list of attribute key value pairs when I dont know the key names or how many attributes there will be?

                  推荐答案

                  您可以使用 XmlAnyAttribute 属性指定任意属性将被序列化和反序列化为 XmlAttribute [] 属性或使用 XmlSerializer 时的字段.

                  You can use the XmlAnyAttribute attribute to specify that arbitrary attributes will be serialized and deserialized into an XmlAttribute [] property or field when using XmlSerializer.

                  例如,如果要将属性表示为 Dictionary,则可以定义 ItemRootNode类如下,使用代理 XmlAttribute[] 属性将字典与所需的 XmlAttribute 数组相互转换:

                  For instance, if you want to represent your attributes as a Dictionary<string, string>, you could define your Item and RootNode classes as follows, using a proxy XmlAttribute[] property to convert the dictionary from and to the required XmlAttribute array:

                  public class Item
                  {
                      [XmlIgnore]
                      public Dictionary<string, string> Attributes { get; set; }
                  
                      [XmlAnyAttribute]
                      public XmlAttribute[] XmlAttributes
                      {
                          get
                          {
                              if (Attributes == null)
                                  return null;
                              var doc = new XmlDocument();
                              return Attributes.Select(p => { var a = doc.CreateAttribute(p.Key); a.Value = p.Value; return a; }).ToArray();
                          }
                          set
                          {
                              if (value == null)
                                  Attributes = null;
                              else
                                  Attributes = value.ToDictionary(a => a.Name, a => a.Value);
                          }
                      }
                  }
                  
                  public class RootNode
                  {
                      [XmlElement("Item")]
                      public List<Item> Items { get; set; }
                  }
                  

                  原型小提琴.

                  这篇关于如何在 C# 中使用属性列表反序列化元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以将属性添加到部分类中的属性? 下一篇:什么时候应该在 C# 中使用属性?

                  相关文章

                  • <bdo id='HSsoS'></bdo><ul id='HSsoS'></ul>
                  1. <i id='HSsoS'><tr id='HSsoS'><dt id='HSsoS'><q id='HSsoS'><span id='HSsoS'><b id='HSsoS'><form id='HSsoS'><ins id='HSsoS'></ins><ul id='HSsoS'></ul><sub id='HSsoS'></sub></form><legend id='HSsoS'></legend><bdo id='HSsoS'><pre id='HSsoS'><center id='HSsoS'></center></pre></bdo></b><th id='HSsoS'></th></span></q></dt></tr></i><div id='HSsoS'><tfoot id='HSsoS'></tfoot><dl id='HSsoS'><fieldset id='HSsoS'></fieldset></dl></div>
                    <legend id='HSsoS'><style id='HSsoS'><dir id='HSsoS'><q id='HSsoS'></q></dir></style></legend>
                  2. <small id='HSsoS'></small><noframes id='HSsoS'>

                      <tfoot id='HSsoS'></tfoot>