如何解析其中具有属性的 XML 元素? Asp.net C#

How to parse an XML element that has an attribute inside it? Asp.net C#

XML 文件:

<weatherdata>
<forecast>

<time day="2017-04-18">
    <temperature day="-4.26" min="-6.54" max="-4.26" night="-6.54" eve="-4.26" morn="-4.26"/>
</time>

<time day="2017-04-19">
    <temperature day="3.51" min="-5.41" max="4.49" night="-0.63" eve="4.27" morn="-5.41"/>
</time>

</forecast>
</weatherdata>

我需要解析文件并获取正确的元素,具体取决于我想使用哪一天="xxxx-xx-xx"。然后,当我有合适的时间时,我想为 temperature_day、temperature_min 等创建具有正确值的新字符串。

我已经尝试了几十种 XmlReader 变体,我想使用它,因为它看起来很简单。这是我到目前为止所拥有的。如果我可以命名 "reader.Name ==" 为什么我不能只要求属性名称呢?

XmlReader reader = XmlReader.Create(URL);
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element && reader.Name == "time" && reader.HasAttributes)
        {
            // DATE comes from a asp.net calendar, the format is correct
            if (DATE == reader.GetAttribute("day"))
            {
               if (reader.NodeType == XmlNodeType.Element && reader.Name == "temperature" && reader.HasAttributes)
                {
                    string temperature_day = reader.GetAttribute("day");
                    TextBoxTemp.Text = temperature_day;
                }
            }
        }
    }

我已经用非常简单的 XmlReader 代码位测试了 (URL) 并确认它可以工作。我现在完全没有想法,元素内部的属性让我很困惑。

使用 XDocument 而不是 XmlReader 更容易查询。

第一个例子

一些代码,您在其中循环遍历了 LINQ 查询 return 的结果。但是因为里面只有一个

       // Create XDocument and load in the file
        XDocument _doc = XDocument.Load("C:\t\My File2.txt");
        //select all time element where attribute day.Value == to any day you give it just change "2017-04-18" 
        var time = _doc.XPathSelectElements("/weatherdata/forecast/time").Where(c => c.Attribute("day").Value == "2017-04-18");
        // Get IEnumerable of temperatures in the time element
        var temp = time.Select(x => x.Element("temperature"));

        // loop temperatures and get the attribute value from the element
        foreach (var element in temp)
        {
            string day = element.Attribute("day").Value;
            string min = element.Attribute("min").Value;
            string max = element.Attribute("max").Value;
            string night = element.Attribute("night").Value;
            string eve = element.Attribute("eve").Value;
            string morn = element.Attribute("morn").Value;
            MessageBox.Show($"day:{day},\nmin:{min},\nmax:{max},\nnight:{night},\neve:{eve},\nmorn:{morn}");
        }

第二个例子

我得到了第一个查询结果,所以你不必循环获取一个元素。这有点冒险,因为当没有找到元素时,您可能会得到空引用异常。但是通过一个简单的如果你能在它被抛出之前抓住它。

         // Create XDocument and load in the file my case it is the path "C:\t\My File2.txt"
        XDocument _doc = XDocument.Load("C:\t\My File2.txt");
        // Select all time elements where attribute day.Value == to any day you give it, just change "2017-04-18" 
        // And select the first one of that result as I aspect there is only one time a day
        var time = _doc.XPathSelectElements("/weatherdata/forecast/time").Where(c => c.Attribute("day").Value == "2017-04-18").FirstOrDefault();
        // Get the children of time element by using .Elements() and take the first one of that.
        // You only have one temperature element in a time element so it will take that one with FirstOrDefault();
        var temp = time.Elements().FirstOrDefault();
        // assign attributes values to string
        string day = temp.Attribute("day").Value;
        string min = temp.Attribute("min").Value;
        string max = temp.Attribute("max").Value;
        string night = temp.Attribute("night").Value;
        string eve = temp.Attribute("eve").Value;
        string morn = temp.Attribute("morn").Value;

        // control
        MessageBox.Show($"day:{day},\nmin:{min},\nmax:{max},\nnight:{night},\neve:{eve},\nmorn:{morn}");

日期为“2017-04-18”的结果: