从 <...> 解析 xml 文件到<.../>

发布于 2025-01-04 20:06:48 字数 1687 浏览 0 评论 0 原文

问题是

我有具有这种结构的 xml 文件

......................
<current_conditions>
  <condition data="partly cloudy"/>
  <temp_f data="2"/>
  <temp_c data="-17"/>
  <humidity data="Huminidy: 66 %"/>
  <icon data="/ig/images/weather/partly_cloudy.gif"/>
  <wind_condition data="Wind: С, 2 м/с"/>
</current_conditions>
<forecast_conditions>
  <day_of_week data=""/>
  <low data="-23"/>
  <high data="-14"/>
  <icon data="/ig/images/weather/mostly_sunny.gif"/>
  <condition data="Mostly sunny"/>
</forecast_conditions>
.....................

,我像这样解析它

               while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element)
                    {
                        if (r.Name == "current_conditions")
                        {
                            string temp = "";
                            while (r.Read() && r.Name!="forecast_conditions")//I've addee this condition because it parse all nodes after "current conditions"
                            {
                                if (Current_Condtions.Contains(r.Name))
                                {
                                    temp += r.GetAttribute("data");
                                    temp += "\n";
                                }
                            }
                            Console.WriteLine(temp);
                        }
                    }
                }

,我添加了条件,但它仍然读取文件到最后,但我只想从 解析到 然后停止读取 xml 文件。 怎么做呢?

The problem is

I've got xml file with such structure

......................
<current_conditions>
  <condition data="partly cloudy"/>
  <temp_f data="2"/>
  <temp_c data="-17"/>
  <humidity data="Huminidy: 66 %"/>
  <icon data="/ig/images/weather/partly_cloudy.gif"/>
  <wind_condition data="Wind: С, 2 м/с"/>
</current_conditions>
<forecast_conditions>
  <day_of_week data=""/>
  <low data="-23"/>
  <high data="-14"/>
  <icon data="/ig/images/weather/mostly_sunny.gif"/>
  <condition data="Mostly sunny"/>
</forecast_conditions>
.....................

I parse it like this

               while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element)
                    {
                        if (r.Name == "current_conditions")
                        {
                            string temp = "";
                            while (r.Read() && r.Name!="forecast_conditions")//I've addee this condition because it parse all nodes after "current conditions"
                            {
                                if (Current_Condtions.Contains(r.Name))
                                {
                                    temp += r.GetAttribute("data");
                                    temp += "\n";
                                }
                            }
                            Console.WriteLine(temp);
                        }
                    }
                }

I've added conditions but It still read file to the end, but I only want to parse from <current_conditions> to </current_conditions> and then stop reading xml file.
How to do it?

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

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

发布评论

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

评论(3

甜中书 2025-01-11 20:06:48

最简单的方法是在获取所需数据后添加 break; 语句。

更干净的方法是使用 ReadSubtree< /code> 方法。 当您位于 current_conditions 节点时,使用它来创建新的读取器。然后它只会读取该节点及其子节点。

像这样的东西

r.ReadToFollowing("current_conditions")
subtree = r.ReadSubtree()
while(subtree.Read())
{
    //Do your stuff with subtree...
}

The easiest way is to add a break; statement after you get the data you need.

A cleaner way would be to use the ReadSubtree method. Use it to create a new reader once you're on the current_conditions node. Then it will only read that node and its children.

Something like

r.ReadToFollowing("current_conditions")
subtree = r.ReadSubtree()
while(subtree.Read())
{
    //Do your stuff with subtree...
}
メ斷腸人バ 2025-01-11 20:06:48

在完成所需操作后,您需要一个 break 语句。

you need a break statement at the point where you've done what you wanted.

笙痞 2025-01-11 20:06:48

尝试使用innerXML 属性。

Try using the innerXML attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文