【问题标题】:How to extract the data from this xml document如何从此 xml 文档中提取数据
【发布时间】:2020-09-15 21:57:49
【问题描述】:

我想从每个行星和小行星(不在此示例中)提取数据并将其放入字符串列表中。 所以一个行星列表将包含(按顺序):

  • 姓名
  • 位置 x
  • 位置 y
  • 半径
  • 速度 x
  • 速度 y (any) - 邻居

我在遍历 xml 时遇到了很多麻烦,而且关于这个 imo 的好的教程也不多。

xml:

<galaxy>
  <planet>
    <name>Kobol</name>
    <position>
      <x>45</x>
      <y>310</y>
      <radius>7</radius>
    </position>
    <speed>
      <x>0.3</x>
      <y>-0.6</y>
    </speed>
    <neighbours>
      <planet>Unicron</planet>
      <planet>Koarth</planet>
    </neighbours>
    <color>blue</color>
    <oncollision>blink</oncollision>
  </planet>
  <planet>
    <name>Namek</name>
    <position>
      <x>60</x>
      <y>102</y>
      <radius>15</radius>
    </position>
    <speed>
      <x>0.1</x>
      <y>0.2</y>
    </speed>
    <neighbours>
      <planet>Helicon</planet>
      <planet>Synnax</planet>
      <planet>Xenex</planet>
      <planet>Alderaan</planet>
    </neighbours>
    <color>orange</color>
    <oncollision>blink</oncollision>
  </planet>
</galaxy>

谁能帮忙?

【问题讨论】:

    标签: c# xml linq loops xmlreader


    【解决方案1】:

    你说的是XML反序列化。走另一条路(对象到xml)称为序列化

    在 StackOverflow 和一般的网络上有很多这两个例子。使用这些术语进行搜索应该会对您有所帮助。

    这是一个来自 StackOverflow 的好消息:

    How to Deserialize XML document

    【讨论】:

      【解决方案2】:

      我们可以通过以下方式定义表示 Xml 的模型:

      [XmlRoot("galaxy")]
      public class Galaxy
      {
          [XmlElement("planet")]
          public List<Planet> Planets { get; set; }
      }
      
      public class Planet
      {
          [XmlElement("name")]
          public string Name { get; set; }
          
          [XmlElement("position")]
          public Position Position { get; set; }
          
          [XmlElement("speed")]
          public Speed Speed { get; set; }
          
          [XmlElement("neighbours")]
          public List<Neighbour> Neighbours { get; set; }
          
          [XmlElement("color")]
          public string Color { get; set; }
          
          [XmlElement("oncollision")]
          public string OnCollision { get; set; }
      }
      
      public class Position
      {
          [XmlElement("x")]
          public double X { get; set; }
      
          [XmlElement("y")]
          public double Y { get; set; }
      
          [XmlElement("radius")]
          public double Radius { get; set; }
      }
      
      public class Speed
      {
          [XmlElement("x")]
          public double X { get; set; }
      
          [XmlElement("y")]
          public double Y { get; set; }
      }
      
      public class Neighbour
      {
          [XmlElement("planet")]
          public string Name { get; set; }
      }
      

      注意我们如何使用属性来定义 Xml 的布局,并将其映射回对象。

      有了这个,我们可以通过以下方式反序列化 Xml:

      XmlSerializer serializer = new XmlSerializer(typeof(Galaxy));
      
      using (FileStream stream = new FileStream(filePath, FileMode.Open))
      {
          Galaxy galaxy = (Galaxy) serializer.Deserialize(stream);
      
          foreach (Planet planet in galaxy.Planets)
          {
              Console.WriteLine(planet.Name);
          }
      }
      

      输出

      Kobol
      Namek
      

      【讨论】:

        猜你喜欢
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        相关资源
        最近更新 更多