草庐IT

c# - 在 C# 中反序列化多个具有相同名称的 XML 元素

coder 2024-07-04 原文

我正在尝试解析此 XML 响应:

    <?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
  <Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location>
  <Time>Feb 11, 2015 - 11:54 AM EST / 2015.02.11 1654 UTC</Time>
  <Wind> from the N (010 degrees) at 17 MPH (15 KT) gusting to 26 MPH (23 KT):0</Wind>
  <Visibility> 2 mile(s):0</Visibility>
  <SkyConditions> overcast</SkyConditions>
  <Temperature> 19.9 F (-6.7 C)</Temperature>
  <Wind>Windchill: 5 F (-15 C):1</Wind>
  <DewPoint> 12.9 F (-10.6 C)</DewPoint>
  <RelativeHumidity> 73%</RelativeHumidity>
  <Pressure> 30.08 in. Hg (1018 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>

如您所见,“Wind”元素出现了两次,这让我很为难,因为我显然不能保存 2 个同名变量。

我已经尝试通过对元素进行编号来解决这个问题,我的反序列化类现在如下所示:

public class CurrentWeather
{
    [XmlElement(Order = 1)]
    public string Location { get;  set; }
    [XmlElement(Order = 2)]
    public string Time { get;  set; }
    [XmlElement(Order = 3)]
    public string Wind { get;  set; }
    [XmlElement(Order = 4)]
    public string Visibility { get;  set; }
    [XmlElement(Order = 5)]
    public string SkyConditions { get;  set; }
    [XmlElement(Order = 6)]
    public string Temperature { get;  set; }
    [XmlElement(Order = 7)]
    public string WindTemperature { get;  set; }
    [XmlElement(Order = 8)]
    public string DewPoint { get;  set; }
    [XmlElement(Order = 9)]
    public string RelativeHumidity { get;  set; }
    [XmlElement(Order = 10)]
    public string Pressure { get;  set; }
    [XmlElement(Order = 11)]
    public string Status { get;  set; }
}

现在问题继续.. 它反序列化所有元素,直到第二个“Wind”正确,在第二个“Wind”(包括)之后,其余元素为“null” 有什么建议吗?

最佳答案

这行得通,只需将 wind 设为一个数组即可:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindDeserialize
{
    using System.IO;
    using System.Xml.Serialization;

    class Program
    {
        public const string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?>                                "+  
"<CurrentWeather>                                                                               "+
"  <Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location> "+
"  <Time>Feb 11, 2015 - 11:54 AM EST / 2015.02.11 1654 UTC</Time>                               "+
"  <Wind> from the N (010 degrees) at 17 MPH (15 KT) gusting to 26 MPH (23 KT):0</Wind>         "+
"  <Visibility> 2 mile(s):0</Visibility>                                                        "+
"  <SkyConditions> overcast</SkyConditions>                                                     "+
"  <Temperature> 19.9 F (-6.7 C)</Temperature>                                                  "+
"  <Wind>Windchill: 5 F (-15 C):1</Wind>                                                        "+
"  <DewPoint> 12.9 F (-10.6 C)</DewPoint>                                                       "+
"  <RelativeHumidity> 73%</RelativeHumidity>                                                    "+
"  <Pressure> 30.08 in. Hg (1018 hPa)</Pressure>                                                "+
"  <Status>Success</Status>                                                                     "+
"</CurrentWeather>                                                                              ";
        static void Main(string[] args)
        {
            XmlSerializer ser = new XmlSerializer(typeof(CurrentWeather));

            var weather = (CurrentWeather)ser.Deserialize(new StringReader(xml));

        }


    }

    public class CurrentWeather
    {
        [XmlElement]
        public string Location { get; set; }
        [XmlElement]
        public string Time { get; set; }
        [XmlElement]
        public string [] Wind { get; set; }
        [XmlElement]
        public string Visibility { get; set; }
        [XmlElement]
        public string SkyConditions { get; set; }
        [XmlElement]
        public string Temperature { get; set; }
        [XmlElement]
        public string DewPoint { get; set; }
        [XmlElement]
        public string RelativeHumidity { get; set; }
        [XmlElement]
        public string Pressure { get; set; }
        [XmlElement]
        public string Status { get; set; }
    }
}

关于c# - 在 C# 中反序列化多个具有相同名称的 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28460766/

有关c# - 在 C# 中反序列化多个具有相同名称的 XML 元素的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  6. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  7. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  8. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  9. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  10. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

随机推荐