WP7的XML读取问题

发布于 2024-11-24 21:58:07 字数 2857 浏览 3 评论 0原文

我正在尝试使用 Windows Phone 7 创建一个应用程序,该应用程序显示来自特定 URI 的数据,但它不起作用。我是堆栈,请帮助我。 这是我的 XML:

<?xml version="1.0" encoding="utf-8" ?>
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
    <forecast_conditions>
        <day_of_week data="lun."/>
        <low data="28"/>
        <high data="38"/>
        <icon data="/ig/images/weather/mostly_sunny.gif"/>
        <condition data="Partiellement ensoleillé"/>
    </forecast_conditions>
    <forecast_conditions>
        <day_of_week data="mar."/>
        <low data="27"/>
        <high data="39"/>
        <icon data="/ig/images/weather/sunny.gif"/>
        <condition data="Temps clair"/>
    </forecast_conditions>
    <forecast_conditions>
        <day_of_week data="mer."/>
        <low data="25"/>
        <high data="38"/>
        <icon data="/ig/images/weather/mostly_sunny.gif"/>
        <condition data="Ensoleillé dans l'ensemble"/>
    </forecast_conditions>
    <forecast_conditions>
        <day_of_week data="jeu."/>
        <low data="24"/>
        <high data="33"/>
        <icon data="/ig/images/weather/mostly_sunny.gif"/>
        <condition data="Ensoleillé dans l'ensemble"/>
    </forecast_conditions>
</weather>

这是我的 C# 代码:

namespace WEATHER2
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructeur
        public MainPage()
        {
            InitializeComponent();
            XDocument doc = XDocument.Load("Gweather.xml");
            var x= from c in doc.Descendants("forecast_conditions")
            select new Weather_Element()
             {
                 Day = (string)c.Attribute("day_of_week").Value,
                 Low = (string)c.Attribute("low").Value,
                 High = (string)c.Attribute("high").Value,
                 Condition = (string)c.Attribute("condition").Value
              };
             listBox1.ItemsSource = x;
        }

        public class Weather_Element
        {
            string day;
            string low;
            string high;
            string condition;

            public string Day
            {
               get { return day; }
               set { day = value; }
            }
            public string Low
            {
               get { return low; }
               set { low = value; }
            }
            public string High
            {
               get { return high; }
               set { high = value; }
            }
            public string Condition
            {
               get { return condition; }
               set { condition = value; }
            }
        }
    }
}

i'm trying to create an application with Windows Phone 7, which displays data from a specific URI, but it won't work. I'm stack,help me please.
This is my XML :

<?xml version="1.0" encoding="utf-8" ?>
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
    <forecast_conditions>
        <day_of_week data="lun."/>
        <low data="28"/>
        <high data="38"/>
        <icon data="/ig/images/weather/mostly_sunny.gif"/>
        <condition data="Partiellement ensoleillé"/>
    </forecast_conditions>
    <forecast_conditions>
        <day_of_week data="mar."/>
        <low data="27"/>
        <high data="39"/>
        <icon data="/ig/images/weather/sunny.gif"/>
        <condition data="Temps clair"/>
    </forecast_conditions>
    <forecast_conditions>
        <day_of_week data="mer."/>
        <low data="25"/>
        <high data="38"/>
        <icon data="/ig/images/weather/mostly_sunny.gif"/>
        <condition data="Ensoleillé dans l'ensemble"/>
    </forecast_conditions>
    <forecast_conditions>
        <day_of_week data="jeu."/>
        <low data="24"/>
        <high data="33"/>
        <icon data="/ig/images/weather/mostly_sunny.gif"/>
        <condition data="Ensoleillé dans l'ensemble"/>
    </forecast_conditions>
</weather>

This is my c# code:

namespace WEATHER2
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructeur
        public MainPage()
        {
            InitializeComponent();
            XDocument doc = XDocument.Load("Gweather.xml");
            var x= from c in doc.Descendants("forecast_conditions")
            select new Weather_Element()
             {
                 Day = (string)c.Attribute("day_of_week").Value,
                 Low = (string)c.Attribute("low").Value,
                 High = (string)c.Attribute("high").Value,
                 Condition = (string)c.Attribute("condition").Value
              };
             listBox1.ItemsSource = x;
        }

        public class Weather_Element
        {
            string day;
            string low;
            string high;
            string condition;

            public string Day
            {
               get { return day; }
               set { day = value; }
            }
            public string Low
            {
               get { return low; }
               set { low = value; }
            }
            public string High
            {
               get { return high; }
               set { high = value; }
            }
            public string Condition
            {
               get { return condition; }
               set { condition = value; }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-12-01 21:58:07

您正在尝试从没有属性的元素中获取属性值。

var x = from c in doc.Descendants("forecast_conditions")
select new Weather_Element()
{
    Day = c.Element("day_of_week").Attribute("data").Value,
    Low = c.Element("low").Attribute("data").Value,
    High = c.Element("high").Attribute("data").Value,
    Condition = c.Element("condition").Attribute("data").Value
};

forecast_conditions 类型的元素 c 有一个元素 day_of_week。那么这个元素有一个属性data

You are trying to get attribute values from the an element without attributes.

var x = from c in doc.Descendants("forecast_conditions")
select new Weather_Element()
{
    Day = c.Element("day_of_week").Attribute("data").Value,
    Low = c.Element("low").Attribute("data").Value,
    High = c.Element("high").Attribute("data").Value,
    Condition = c.Element("condition").Attribute("data").Value
};

The element c of type forecast_conditions has an element day_of_week. Then this element has an attribute data.

_畞蕅 2024-12-01 21:58:07

您的 forecast_conditions 没有任何属性,它们具有子元素,而子元素则具有 data 属性。所以而不是

        var x= from c in doc.Descendants("forecast_conditions")
        select new Weather_Element()
         {
             Day = (string)c.Attribute("day_of_week").Value,
             Low = (string)c.Attribute("low").Value,
             High = (string)c.Attribute("high").Value,
             Condition = (string)c.Attribute("condition").Value
          };

使用

        var x= from c in doc.Descendants("forecast_conditions")
        select new Weather_Element()
         {
             Day = (string)c.Element("day_of_week").Attribute("data"),
             Low = (string)c.Element("low").Attribute("data"),
             High = (string)c.Element("high").Attribute("data"),
             Condition = (string)c.Element("condition").Attribute("data")
          };

Your forecast_conditions don't have any attributes, they have child elements instead which then have data attributes. So instead of

        var x= from c in doc.Descendants("forecast_conditions")
        select new Weather_Element()
         {
             Day = (string)c.Attribute("day_of_week").Value,
             Low = (string)c.Attribute("low").Value,
             High = (string)c.Attribute("high").Value,
             Condition = (string)c.Attribute("condition").Value
          };

use

        var x= from c in doc.Descendants("forecast_conditions")
        select new Weather_Element()
         {
             Day = (string)c.Element("day_of_week").Attribute("data"),
             Low = (string)c.Element("low").Attribute("data"),
             High = (string)c.Element("high").Attribute("data"),
             Condition = (string)c.Element("condition").Attribute("data")
          };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文