I try many different solution on this site and none seems to work for me .
I getting a xml file from a website and it's returned to me in a string.
using the code below I need to read the nodes in the "entry" section of the xml file.
but it always comes up "0" meaning no nodes found. the only thing left I think is the XML file is not correct?
any help would be great...
------------------code below ------------:
//gets the xml file
string WeatherXML = HttpPost("http://weather.gov/alerts-beta/wwaatmget.php?x=MIC159", "");
//create a xmldoc object..
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
//load the object with the xml file from the web...
doc.LoadXml(WeatherXML);
//go to the main node..
XmlNodeList nodes = doc.SelectNodes("/feed/entry");
//I also tried ....
//doc.SelectNodes("//feed/entry");
//doc.SelectNodes("/entry");
//doc.SelectNodes("//entry");
//loop through the nodes (here is where the nodelist is always empty..
foreach (XmlNode node in nodes)
{
string msgType = node["cap:msgType"].InnerText;
string areaDesc = node["cap:areaDesc"].InnerText;
string summary = node["summary"].InnerText;
string title = node["title"].InnerText;
string link = node["link"].InnerText;
}
------------------------------XML file below------------------
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'no'?>
<!--
This atom/xml feed is an index to active advisories, watches and warnings issued
by the National Weather Service. This index file is not the complete Common
Alerting Protocol (CAP) alert message. To obtain the complete CAP alert,
please follow the links for each entry in this index. Also note the CAP
message uses a style sheet to convey the information in a human readable
format. Please view the source of the CAP message to see the complete data
set. Not all information in the CAP message is contained in this index of
active alerts.
-->
<feed
xmlns = 'http://www.w3.org/2005/Atom'
xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
>
<!-- http-date = Sun, 22 Aug 2010 07:06:00 GMT -->
<id>http://www.weather.gov/alerts-beta/wwaatmget.php?x=MIC159</id>
<generator>
NWS CAP Server
</generator>
<updated>2010-08-22T19:06:00-04:00</updated>
<author>
<name>
w-nws.webmaster#noaa.gov
</name>
</author>
<title>
Current Watches, Warnings and Advisories for Van Buren (MIC159) Michigan Issued by the National Weather Service
</title>
<link href='http://www.weather.gov/alerts-beta/wwaatmget.php?x=MIC159'/>
<entry>
<id>http://www.weather.gov/alerts-beta/wwacapget.php?x=MI20100822190600IWXRipCurrentStatementIWX20100823060000MI</id>
<updated>2010-08-22T15:06:00-04:00</updated>
<published>2010-08-22T15:06:00-04:00</published>
<author>
<name>w-nws.webmaster#noaa.gov</name>
</author>
<title>Rip Current Statement issued August 22 at 3:06PM EDT expiring August 23 at 2:00AM EDT by NWS NorthernIndiana http://www.crh.noaa.gov/iwx/</title>
<link href="http://www.weather.gov/alerts-beta/wwacapget.php?x=MI20100822190600IWXRipCurrentStatementIWX20100823060000MI"/>
<summary>...RIP CURRENT RISK REMAINS IN EFFECT UNTIL 2 AM EDT /1 AM CDT/ MONDAY... ...HIGH RISK OF RIP CURRENTS... HIGH WAVES ALONG THE SHORELINE WILL BRING AN INCREASED RISK OF RIP CURRENTS INTO THE EARLY MORNING HOURS OF MONDAY...CREATING DANGEROUS SWIMMING CONDITIONS.</summary>
<cap:effective>2010-08-22T15:06:00-04:00</cap:effective>
<cap:expires>2010-08-23T02:00:00-04:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency></cap:urgency>
<cap:severity></cap:severity>
<cap:certainty></cap:certainty>
<cap:areaDesc>Berrien; Cass; La Porte; St. Joseph; Van Buren</cap:areaDesc>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>018091 018141 026021 026027 026159</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.CON.KIWX.RP.S.0017.000000T0000Z-100823T0600Z/</value>
</cap:parameter>
</entry>
</feed>:"
It’s because your XML root node has a namespace. The following should work:
//load the object with the xml file from the web...
doc.LoadXml(WeatherXML);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("m", "http://www.w3.org/2005/Atom");
//go to the main node..
XmlNodeList nodes = doc.SelectNodes("m:feed", nsMgr);
Console.WriteLine(nodes.Count); // outputs 1
Add the "http://www.w3.org/2005/Atom" namespace to the XPath using an XmlNamespaceManager.
Instead of using regular System.Xml classes you could also use classes from the System.Xml.Linq namespace. Personally I find these a lot easier to use.
var doc = XDocument.Parse(WeatherXml);
var entryNodes = doc.Descendants(
XName.Get("entry", "http://www.w3.org/2005/Atom"));
This will give you a collection of entry nodes from the document.
Related
I want to read all the nodes in the xml. Reading the top node without the loop goes fine, but I want to read all the nodes to get all the different temperatures for each city. This is the xml:
<servers>
<server name ="Stockholm">
<!-- data from Google weather -->
<name>Stockholm (Google)</name><!--Läser denna rad för tillfället-->
<url>http://www.yr.no/place/sweden/stockholm/stockholm/forecast.xml</url>
<xpath>/weatherdata/forecast/tabular/time/temperature</xpath>
</server>
<server name ="Göteborg">
<name>Göteborg (Google)</name> <url>http://www.yr.no/place/Sweden/V%C3%A4stra_G%C3%B6taland/Gothenburg/forecast.xml</url>
<xpath>/weatherdata/forecast/tabular/time/temperature</xpath>
</server>
<server name =" Malmö">
<name>Malmö (Google)</name>
<url>http://www.yr.no/place/sweden/scania/malmö/forecast.xml</url>
<xpath>/weatherdata/forecast/tabular/time/temperature</xpath>
</server>
</servers>
My code so far:
XmlDocument serverDoc = new XmlDocument();
serverDoc.Load("ServerUrls.xml");
XmlNodeList xml =
serverDoc.SelectNodes("servers/server");
foreach (var node in xml)
{
}
I know its not much. But cant seem to find information I can use properly. Been to MSDN and tried to figure it out from there, but to no result.
Thankful for all the help I can get.
As mentioned in the comment, you need to get URL of the XML where the actual temperature resides, as well as the corresponding XPath to locate the temperatures within the XML. Load XML from the URL into XmlDocument and then execute the XPath to extract the actual temperature values :
foreach (XmlNode node in xml)
{
// get information needed to extract temprature i.e XML location, and the XPath :
var loc = node.SelectSingleNode("url").InnerText;
var xpath = node.SelectSingleNode("xpath").InnerText;
// get temperature information
var doc = new XmlDocument();
doc.Load(loc);
var temperatures = doc.SelectNodes(xpath);
// iterate through temperatures and take action accordingly, f.e print the temperature
foreach(XmlNode temperature in temperatures)
{
Console.WriteLine(temperature.Attributes["value"].Value);
}
}
Good Evening All, and happy weekend!.
I have been trying all day to understand how to parse my simple XML file so I can understand it enough to write a personal project I want to work on.
I have been reading articles on this site and others but cannot get past where I am :(
My XML Document is ...
<XML>
<User>
<ID>123456789</ID>
<Device>My PC</Device>
</User>
<History>
<CreationTime>27 June 2013</CreationTime>
<UpdatedTime>29 June 2013</UpdatedTime>
<LastUsage>30 June 2013</LastUsage>
<UsageCount>103</UsageCount>
</History>
<Configuration>
<Name>Test Item</Name>
<Details>READ ME</Details>
<Enabled>true</Enabled>
</Configuration>
</XML>
I am trying to get the value in the details element (READ ME). Below is my code
// Start Logging Progress
Console.WriteLine("Test Application - XML Parsing and Creating");
Console.ReadKey();
// Load XML Document
XmlDocument MyDoc = new XmlDocument(); MyDoc.Load(#"E:\MyXML.XML");
// Select Node
XmlNode MyNode = MyDoc.SelectSingleNode("XML/Configuration/Details");
// Output Node Value
Console.WriteLine(String.Concat("Details: ", MyNode.Value));
// Pause
Console.ReadKey();
My console application is running and outputing "Target: " but not giving me the detail within the element.
Can somebody see why this is happening, and perhaps give me advice if I am completely off the wheel? I have no previous knowledge in reading XML files; hence where I am now :)
Thanks! Tom
With the your XPATH expression
// Select Node
XmlNode MyNode = MyDoc.SelectSingleNode("XML/Configuration/Details");
your are selection an element so the type of the MyNode will be XmlElement but the Value of an XmlElement is always null (see on MSDN) so you need to use XmlElement.InnerText or XmlElement.InnerXml isntead.
So the changed your code to
// Output Node Value
Console.WriteLine(String.Concat("Details: ", MyNode.InnerText));
Or you can select the content of an element with using the XPATH text() function, in this case MyNode will be XmlText where you get its value with Value:
// Select Node
XmlNode MyNode = MyDoc.SelectSingleNode("XML/Configuration/Details/text()");
// Output Node Value
Console.WriteLine(String.Concat("Details: ", MyNode.Value));
As a sidenote if you are anyway learning XML manipulation in C# you should check out LINQ to XML which is another/newer way to working with XML in C#.
Just for interest, a little-known "simple" syntax is this:
XmlDocument myDoc = new XmlDocument();
myDoc.Load(#"D:\MyXML.XML");
string details = myDoc["XML"]["Configuration"]["Details"].InnerText;
Note that this (and the XPath approach) could go pop if your XML doesn't conform to the structure you're expecting, so you'd ideally put some validation in there as well.
U can use Xpath library for that (u must include "System.Xml.XPath"):
XmlDocument document = new XmlDocument();
document.Load("MyXml.xml");
XPathNavigator navigator = document.CreateNavigator();
foreach (XPathNavigator nav in navigator.Select("//Details"))
{
Console.WriteLine(nav.Value);
}
the above code iterate over every node called (Details) extracting information and print it.
If you want to retrieve a particular value from an XML file
XmlDocument _LocalInfo_Xml = new XmlDocument();
_LocalInfo_Xml.Load(fileName);
XmlElement _XmlElement;
_XmlElement = _LocalInfo_Xml.GetElementsByTagName("UserId")[0] as XmlElement;
string Value = _XmlElement.InnerText;
Value contains the text value
I have an cXML that I am trying to read some values from....
I can read the node values using following code. But I am having hard time
reading the "payloadID" from the same document. Any suggestions how to go about it?
XmlDocument xmlRequest = new XmlDocument();
XmlNodeList name = xmlRequest.GetElementsByTagName("NetworkID");
string sSecret = name[0].InnerText;
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.023/cXML.dtd">
<cXML payloadID="1348609345562-715426217594539696#216.109.111.69"
timestamp="2012-09-11T11:55:53-07:00" version="1.2.023"
xml:lang="en-US">
<From>
<Credential domain="NetworkID">
<Identity>tnt</Identity>
</Credential>
</From>
<To>
<Credential domain="NetworkID1">
<Identity>abc</Identity>
</Credential>
</To>
</cXML>
You should be able to get the root element and then use its Attributes property to read the attributes of that root element, e.g.:
XmlNode root = doc.SelectSingleNode("/cXML");
string attrVal = root.Attributes["payloadID"].Value;
I am working on my project, I am totally new to C# and I need to use C# to do my work. I really hope you guys can lend me a hand in the below issue. Your help will be very much appreciated!! Thanks.
I have an XML file which has two <item>...</item> in parent <channel>. I executed my code, what I get is only as below - data of one <item> appeared:
Title: Re: My view of Tesco
Desciption: Stay clear of the iii IPO when it comes onto the market.
3 quarters are multiples, triples, quadrupole, W-T-F. It´s like ebay
a lot bidding there, is fake too.
Today´s thought of the day: Odd is that Deloitte seems to have
escaped headlines. Accusation it colluded with Standard Chartered on
regulatory report cd be Enron moment Remember those old accountants
Deloitte, Mr. Hyman (RBS)? By Hardcore Uproar
Date: Tue, 07 Aug 2012 14:03:00 GMT
Author: Hardcore Uproar
My code is as below:
private void btnComSearch_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("tsco.xml");
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("item");
foreach (XmlNode node in nodes)
{
XmlNodeList comTitle = xmlDoc.GetElementsByTagName("title");
XmlNodeList comDesc = xmlDoc.GetElementsByTagName("description");
XmlNodeList comDate = xmlDoc.GetElementsByTagName("pubDate");
XmlNodeList comAuthor = xmlDoc.GetElementsByTagName("creator");
StringBuilder sb = new StringBuilder();
sb.AppendLine("Title: " + comTitle[0].InnerText + "\n");
sb.AppendLine("Desciption: " + comDesc[0].InnerText + "\n");
sb.AppendLine("Date: " + comDate[0].InnerText + "\n");
sb.AppendLine("Author: " + comAuthor[0].InnerText + "\n" + "---------------" + "\n");
richComResults.Text = sb.ToString();
}
}
My XML file:
<channel>
<item>
<title>Re: My view of Tesco</title>
<description>
<![CDATA[ Stay clear of the iii IPO when it comes onto the market. 3 quarters are multiples, triples, quadrupole, W-T-F. It´s like ebay a lot bidding there, is fake too.Today´s thought of the day: Odd is that Deloitte seems to have escaped headlines. Accusation it colluded with Standard Chartered on regulatory report cd be Enron moment Remember those old accountants Deloitte, Mr. Hyman (RBS)? By Hardcore Uproar ]]>
</description>
<pubDate>Tue, 07 Aug 2012 14:03:00 GMT</pubDate>
<creator>Hardcore Uproar</creator>
</item>
<item>
<title></title>
<description>
<![CDATA[ Raw material inflation;
Rising (relative)
wealth outside of EU.
Increased global demand for agri-commodities due to increasing population and relative wealth of Eastern countries.
Decoupling of subsidy from agri-production = bad for supermarkets.
Weather problems, diminished /(ing) resources and a general plateau reached in agriculture in terms of yield achievable = limited supply.
Over supply of supermarkets/ retailers (too much choice= supply>demand)
Diminished disposable income;
General recession.
Poor pension performance.
Over indebtidness in UK (further compounded by any increases in interest rates required to curb inflation).
All this is bad news for supermarkets.. in my locality in a farily small town of 14,000 people we have a large ASDA, huge TESCO and M and S and numerous discounters.. they must be counting on all 14000 of those people visiting ALL of their local supermarkets at least 9 times a week IMHO!!
By t8vet ]]>
</description>
<pubDate>Mon, 06 Aug 2012 18:47:00 GMT</pubDate>
<creator>t8vet</creator>
</item>
</channel>
My Edited Code after applying your (horgh) codes:
private void btnComSearch_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
xmlDoc.Load("tsco.xml"); //* load the XML document from the specified file.
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("item"); // You can also use XPath here
foreach (XmlNode node in nodes)
{
StringBuilder sb = new StringBuilder();
foreach (XmlNode child in node.ChildNodes)
sb.AppendLine(string.Format("{0}:\t{1}", child.Name, child.FirstChild == null ? string.Empty : child.FirstChild.Value));
richComResults.Text = sb.ToString();
}
Console.ReadLine();
}
I am totally lost. I tried looking through the websites and also those were asking similar questions, but I don't really understand and I tried they don't work in my situation. I am not sure what have I done wrongly. Your help would be much appreciated :) Thank you so much.
You are watching a xmlDoc, while you should watch each node. Try this:
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
xmlDoc.Load("tsco.xml"); //* load the XML document from the specified file.
richComResults.Text = string.Empty;
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("item"); // You can also use XPath here
StringBuilder sb = new StringBuilder();
foreach (XmlNode node in nodes)
{
foreach (XmlNode child in node.ChildNodes)
sb.AppendLine(string.Format("{0}:\t{1}", child.Name, child.FirstChild == null ? string.Empty : child.FirstChild.Value));
}
richComResults.Text = sb.ToString();
I'm trying to find the inner text value of an element using LINQ-to-XML (an XElement object). I make my service call and get an XML response back that I've successfully loaded into an XElement object. I want to extract the inner text of one of the elements - however, every time I try to do this, I get a null result.
I feel like I'm missing something super-simple, but I'm fairly new to LINQ-to-XML. Any help is appreciated.
I'm trying to get the inner text value of the StatusInfo/Status element. Here's my XML document that's returned:
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
<title type="text">My Response</title>
<id>tag:foo.com,2012:/bar/06468dfc-32f7-4650-b765-608f2b852f22</id>
<author>
<name>My Web Services</name>
</author>
<link rel="self" type="application/atom+xml" href="http://myServer/service.svc/myPath" />
<generator uri="http://myServer" version="1">My Web Services</generator>
<entry>
<id>tag:foo.com,2012:/my-web-services</id>
<title type="text" />
<updated>2012-06-27T14:22:42Z</updated>
<category term="tag:foo.com,2008/my/schemas#system" scheme="tag:foo.com,2008/my/schemas#type" />
<content type="application/vnd.my.webservices+xml">
<StatusInfo xmlns="tag:foo.com,2008:/my/data">
<Status>Available</Status> <!-- I want the inner text -->
</StatusInfo>
</content>
</entry>
</feed>
Here's a snippet of code that I'm using to extract the value (which doesn't work):
XElement root = XElement.Load(responseReader);
XNamespace tag = "tag:foo.com,2008:/my/data";
var status = (from s in root.Elements(tag + "Status")
select s).FirstOrDefault();
My status variable is always null. I've tried several variations on this, but to no avail. The part that's confusing me is the namespace -- tag and 2008 are defined. I don't know if I'm handling this correctly or if there's a better way to deal with this.
Also, I don't have control over the XML schema or the structure of the XML. The service I'm using is out of my control.
Thanks for any help!
Try Descendants() instead of Elements():
XElement x = XElement.Load(responseReader);
XNamespace ns = "tag:foo.com,2008:/my/data";
var status = x.Descendants(ns + "Status").FirstOrDefault().Value;
There are 2 Namespaces in the feed:
the Atom namespace
the tag namespace
The outer xml needs to use the Atom namespace, while a portion of the inner xml needs to use the tag namespace. i.e.,
var doc = XDocument.Load(responseReader);
XNamespace nsAtom = "http://www.w3.org/2005/Atom";
XNamespace nsTag = "tag:foo.com,2008:/my/data";
// get all entry nodes / use the atom namespace
var entry = doc.Root.Elements(nsAtom + "entry");
// get all StatusInfo elements / use the atom namespace
var statusInfo = entry.Descendants(nsTag + "StatusInfo");
// get all Status / use the tag namespace
var status = statusInfo.Elements(nsTag + "Status");
// get value of all Status
var values = status.Select(x => x.Value.ToString()).ToList();