解析XML无法正常工作

时间:2012-02-28 21:30:01

标签: c# asp.net xml

我正在尝试将此xml文件解析为对象以使用它收集的信息。我附上了一个xml的例子。 XDocument填充了xml信息,但无法获取我想要的信息。有人可以帮助我,我是新手吗?

编辑 - 仅仅因为你没有看到我正在寻找的字段并不意味着它不存在。我仔细检查了所有值,它们确实存在,Bill对象非常大,所以我只放了一部分。

示例xml

<results>


<count type="integer">13683</count>
  <bills type="array">
    <bill>
      <abbreviated type="boolean">false</abbreviated>
      <actions type="array">
        <action>
          <type>vote</type>
          <acted_at type="datetime">2010-12-22T12:00:00Z</acted_at>
          <text>Introduced in the Senate, read twice, considered, read the third time, and passed without amendment by Unanimous Consent.</text>
        </action>
        <action>
          <type>action</type>
          <acted_at type="datetime">2010-12-22T12:00:00Z</acted_at>
          <text>Message on Senate action sent to the House.</text>
        </action>
        <action>
          <type>action</type>
          <acted_at type="datetime">2010-12-22T21:05:00Z</acted_at>
          <text>Received in the House.</text>
        </action>
        <action>
          <type>action</type>
          <acted_at type="datetime">2010-12-22T22:10:00Z</acted_at>
          <text>Held at the desk.</text>
        </action>
      </actions>
      <awaiting_signature type="boolean">false</awaiting_signature>
      <bill_id>s4053-111</bill_id>
      <bill_type>s</bill_type>
      <chamber>senate</chamber>
      <code>s4053</code>
      <committees></committees>
      <cosponsor_ids type="array">
        <cosponsor_id>S000663</cosponsor_id>
      </cosponsor_ids>

我的代码(我已尝试将“账单”和“账单”作为后代。)

var tutorials = from tutorial in xmlDoc.Descendants("bills")
                            select new
                            {
                                SponsorID = tutorial.Element("sponsor_id").Value,
                                SponsorFirstName = tutorial.Element("sponsor").Element("first_name").Value,
                                SponsorLastName = tutorial.Element("sponsor").Element("last_name").Value,
                                LastActionText = tutorial.Element("last_action").Element("text").Value,
                                BillNumber = tutorial.Element("number").Value,
                                BillType = tutorial.Element("bill_type").Value,
                                Enacted = tutorial.Element("enacted").Value,
                                EnactedDateTime = tutorial.Element("enacted_at").Value,
                                CongressSession = tutorial.Element("session").Value,
                                HousePassageResult = tutorial.Element("house_passage_result").Value,
                                HousePassageDateTime = tutorial.Element("house_passage_result_at").Value,
                                SenatePassageResult = tutorial.Element("senate_passage_result").Value,
                                SenatePassageDateTime = tutorial.Element("senate_passage_result_at").Value,
                                ShortTitle = tutorial.Element("short_title").Value,
                            };

1 个答案:

答案 0 :(得分:4)

Xml中没有sponsor_idsponsorfirst_name元素(依此类推),所以基本上你的Xml与你的代码所期望的完全不同 - 这个无论“账单”和“账单”如何都无法运作。