帮助读取带命名空间的XML [英] Help with reading XML with namespaces

查看:71
本文介绍了帮助读取带命名空间的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从在线读取XML文件,但它在开始时有一个命名空间,我之前没有遇到过,也不知道如何读取该文件。我的Xpath在没有命名空间的情况下工作,我在线查看并发现您需要使用命名空间管理器。我试过,但命名空间没有你需要指定命名空间的前缀,所以我真的不知道现在该做什么。有人可以帮助我让这段代码工作:

I am trying to read an XML file from online but it has a namespace at the start which I have not come across before and don't know how to read the file. My Xpath works without the namespace and I have looked online and found that you need to use a namespace manager. I tried that but the namespace does not have a prefix which you need to specify a namespace so I don't really know what to do now. Could someone please help me get this code to work:

Console.WriteLine("This program displays a forecast for a region in the UK");
            Console.Write("Enter region ID: ");
            string s = Console.ReadLine();
            while (!Regex.IsMatch(s,"[5-5][0-1][0-6]"))
            {
                Console.Write("Enter region ID: ");
                s = Console.ReadLine();
            }
            Console.WriteLine("");

            XmlDocument forecast = new XmlDocument();            
            forecast.Load("http://datapoint.metoffice.gov.uk/public/data/txt/wxfcs/regionalforecast/xml/" + s + "?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");            
            Console.WriteLine("----- Information -----------------");
            Console.WriteLine("Created : " + forecast.SelectSingleNode("//@createdOn").InnerText);
            Console.WriteLine("Issued  : " + forecast.SelectSingleNode("//@issuedAt").InnerText);
            Console.WriteLine("----- Forecast --------------------");
            Console.WriteLine("Headline : " + forecast.SelectSingleNode("//Period[@id='day1to2']/Paragraph[@title='Headline:']").InnerText);

            Console.ReadKey();



...阅读此XML文件


...To read this XML file

<RegionalFcst xmlns="www.metoffice.gov.uk/xml/metoRegionalFcst" createdOn="2013-10-31T03:32:02" issuedAt="2013-10-31T04:00:00" regionId="wm">
<FcstPeriods>
<Period id="day1to2">
<Paragraph title="Headline:">
A bright start but scattered blustery showers later.
</Paragraph>
<Paragraph title="Today:">
Some bright or sunny spells this morning and becoming breezy. Scattered showers will spread from the west by midday, becoming increasingly frequent through the afternoon and occasionally heavy. Maximum Temperature 13C.
</Paragraph>
<Paragraph title="Tonight:">
A mostly dry night with clear spells, lighter winds and a few mist patches. Cloud increasing during the early hours though with rain possible in southern counties. Minimum Temperature 6C.
</Paragraph>
<Paragraph title="Friday:">
Bright spells possible at first but soon turning cloudy with outbreaks of rain spreading north, perhaps more persistent and heavy later in the afternoon. Winds will stay light. Maximum Temperature 13C.
</Paragraph>
</Period>
<Period id="day3to5">
<Paragraph title="Outlook for Saturday to Monday:">
Strong winds over the weekend with bright spells and showery outbreaks of rain, heaviest on Saturday. Unsettled on Monday with more persistent rain likely.
</Paragraph>
</Period>
<Period id="day6to15">
<Paragraph title="UK Outlook for Tuesday 5 Nov 2013 to Thursday 14 Nov 2013:">
Cloud and rain expected to quickly spread eastwards across the UK on Tuesday, the heaviest rain in the northwest, where gales are likely. On Wednesday, further cloud and rain in the northwest, with occasional rain but also some drier, brighter interludes towards the south and east. Turning milder, especially in the south and east. Largely unsettled conditions thereafter with showers or longer spells of rain, locally heavy, particularly in the northwest, where it will also be windy at times. There will, however, be some drier and brighter interludes, the best of these in the south and east. Remaining generally mild for the time of year. Later in the period, these more settled conditions may last longer in the south resulting in chilly nights here, with more overnight fog.
</Paragraph>
</Period>
<Period id="day16to30">
<Paragraph title="UK Outlook for Friday 15 Nov 2013 to Friday 29 Nov 2013:">
Early November's mostly unsettled conditions across northern and western regions are thought likely to persist through much of the rest of the month. As such, rainfall amounts here are more likely than not to be above average and conditions may also be quite windy at times. Further south and east, rainfall accumulations are considered likely to be nearer average, implying slightly more settled conditions with drier, brighter periods interspersed with occasional bouts of unsettled weather. Drier periods here may, however, be accompanied by overnight and morning fog patches. Taking the country as a whole, temperatures during this period are more likely than not to be near, or perhaps above average throughout, leading to a lower risk of overnight frost than can usually be expected at this time of year.
</Paragraph>
</Period>
</FcstPeriods>
</RegionalFcst>

推荐答案

没有前缀的命名空间是默认命名空间。只是因为他们没有给它一个前缀并不意味着你不能在你的NamespaceManager中给它一个,但是:

That namespace with no prefix is the default namespace. Just because they didn't give it a prefix doesn't mean you can't give it one in your NamespaceManager, though:
System.Xml.XmlNamespaceManager nsm = new System.Xml.XmlNamespaceManager(forecast.NameTable);
nsm.AddNamespace("x", "www.metoffice.gov.uk/xml/metoRegionalFcst");
MessageBox.Show(forecast.SelectSingleNode("//x:Period[@id='day1to2']/x:Paragraph[@title='Headline:']", nsm).OuterXml);


查看下面的文章。 Linq to XML应该相当简单



http://blogs.msdn.com/b/wriju/archive/2008/05/29/linq-to-xml-querying-xml -with-namespaces.aspx [ ^ ]
Have a look into the below article. It should be fairly simple with Linq to XML

http://blogs.msdn.com/b/wriju/archive/2008/05/29/linq-to-xml-querying-xml-with-namespaces.aspx[^]


这篇关于帮助读取带命名空间的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆