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

时间:2017-04-18 20:37:53

标签: c# asp.net xml parsing xmlreader

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>

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

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

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),并确认它可以正常工作。我现在完全没有想法,INSIDE元素属性让我感到困惑。

1 个答案:

答案 0 :(得分:0)

使用XDocument代替XmlReader更容易查询。

第一个例子

一些代码,你通过从LINQ查询返回的结果循环。但是因为你只有一个&lt;时间&gt;元素和一个&lt;温度>里面的元素你可以更好地使用第二个例子

       // 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”:

enter image description here