I'm trying to get the views of a video from the following XML document (https://gdata.youtube.com/feeds/api/videos?q=example), I'm able to get the link and the autor because there is no colon in the tag.
I'm trying to get yt:statistics but I've no idea how.
result = e.Result.Replace("xmlns='http://www.w3.org/2005/Atom' ", String.Empty);
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
XmlNodeList videos = doc.GetElementsByTagName("entry");
foreach (XmlNode video in videos)
{
XmlNode insideauthor = video.SelectSingleNode("author");
string videoId = video.SelectSingleNode("id").InnerText.Replace("http://gdata.youtube.com/feeds/api/videos/", String.Empty);
string author = insideauthor.SelectSingleNode("name").InnerText;
// Trying to get the views of a video of the search results
MessageBox.Show(video.SelectSingleNode("yt:statistics").Attributes["viewCount"].InnerText);
}
XmlNodeList videos = doc.GetElementsByTagName("entry");
foreach (XmlNode video in videos)
{
string videoId = video["id"].InnerText.Replace("http://gdata.youtube.com/feeds/api/videos/", String.Empty);
string author = video["author"]["name"].InnerText;
string views = video["yt:statistics"].Attributes["viewCount"].Value;
Console.WriteLine(videoId);
Console.WriteLine(author);
Console.WriteLine(views);
}
Related
static void Main(string[] args)
{
WebClient _httpReq = new WebClient(); // to talk to the web only for get method
string response = _httpReq.DownloadString("https://open-ic.epic.com/FHIR/api/FHIR/DSTU2/Patient?family=Argonaut&given=Jason");
Console.WriteLine(response);\\prints the xml string fetched from FHIR provider EPIC
XmlDocument xml = new XmlDocument();
xml.LoadXml(response); // suppose that myXmlString contains "<Names>...</Names>"
XmlNodeList xnList = xml.SelectNodes("/entry/resource/patient/name");
// here code is trying to extract the name attribute
foreach (XmlNode xn in xnList)
{
string firstName = xn["family value"].InnerText;
string lastName = xn["given value"].InnerText;
Console.WriteLine("Name: {0} {1}", firstName, lastName);
//print the first name and last name of the patient
}
Console.ReadLine();
}
i do it like this:
XmlDocument MyDocument = new XmlDocument();
MyDocument.Load("...");
XmlNode MyNode = MyDocument.SelectSingleNode("/Node_Name");
foreach (XmlAttribute MyAttribute in MyNode.Attributes)
{
if (MyAttribute.Name == "Attribute_Name")
{
object Value = MyAttribute.Value;
break;
}
}
Review XPath. Once you start understanding it, you will find it efficient and easier to code than iterating through lists. It also lets you directly get the nodes/attributes you want.
Then the code would be something similar to
string attrVal = doc.SelectSingleNode("/entry/resource/patient/#name").Value;
Or you can Load XML in XMlDocument and You can fetch element and out of that element you can read specific atairbute,
XmlDocument doc = new XmlDocument();
doc.LoadXml("<reply success=\"true\">More nodes go here</reply>");
XmlElement root = doc.DocumentElement;
string s = root.Attributes["success"].Value;
I am writing a program that reads a XML file with Visual C#. I have a problem reading the Xml file, because it contains invalid XML symbols, for example '&'.
I have to read the XML but I can not modify the document. How can I modify the Xml file using C#? My code so far:
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc;
doc = new XmlDocument();
doc.Load("nuevo.xml");
XmlNodeList Xpersonas = doc.GetElementsByTagName("personas");
XmlNodeList Xlista = ((XmlElement)Xpersonas[0]).GetElementsByTagName("edad");
foreach (XmlElement nodo in Xlista)
{
string edad = nodo.GetAttribute("edad");
string nombre = nodo.InnerText;
textBox1.Text = nodo.InnerXml;
}
As #EBrown suggested, one possibility would be read the file content in a string variable and replace the & symbol with the correct representation for propert XML & and then parse the XML structure. A possible solution could look like this:
var xmlContent = File.ReadAllText(#"nuevo.xml");
XmlDocument doc;
doc = new XmlDocument();
doc.LoadXml(xmlContent.Replace("&", "&"));
XmlNodeList Xpersonas = doc.GetElementsByTagName("personas");
XmlNodeList Xlista = ((XmlElement)Xpersonas[0]).GetElementsByTagName("edad");
foreach (XmlElement nodo in Xlista)
{
string edad = nodo.GetAttribute("edad");
string nombre = nodo.InnerText;
Console.WriteLine(nodo.InnerXml.Replace("&", "&"));
}
The output is:
34 & 34
If it is ok to use LINQ2XML, then the solution is even shorter, and there is no need to write the reverse(second) replace, because LINQ2XML make this for you automatically:
var xmlContent = File.ReadAllText(#"nuevo.xml");
var xmlDocument = XDocument.Parse(xmlContent.Replace("&", "&"));
var edad = xmlDocument.Root.Element("edad").Value;
Console.WriteLine(edad);
The output is the same as above.
I want to read an XML file and match tag </contrib-group> and write a string after this tag
string Final = File.ReadAllText(Npath);
string Oxml = path + "\\" + Oword + ".abs.xml";
if (File.Exists(Oxml))
{
StreamReader xml = new StreamReader(Oxml,Encoding.UTF8);
string xmltag = xml.ReadToEnd();
//File.OpenWrite(Oxml);
xml.Close();
StreamWriter write = new StreamWriter(Oxml, true, Encoding.UTF8);
Match tag = Regex.Match(xmltag, #"</contrib-group>");
if (tag.Success == true)
{
write.WriteLine(Environment.NewLine);
write.Write(Final);
}
}
So I need to write the string Final to the XML file called Oxml after the matched XML tag </contrib-group>
If you are willing to save the new content as a valid XML file which you can work with, you should use the XML classes for that approach, this should look like this (untested):
XmlDocument doc = new XmlDocument();
doc.Load("YourFile.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("contrib-group");
for (int i=0; i < elemList.Count; i++)
{
XmlNode xnode = elemList[i];
XmlNode xnodeParent = xnode.ParentNode;
XMLNode newNode = doc.CreateNode(XmlNodeType.Element, "NodeName", "");
newNode.InnerText = "ContentInsideTheNode";
xnodeParent.InsertAfter(newNode, xnode);
}
doc.Save("YourFile.xml");
If you only need to replace the string for other purposes than saving it where having a valid XML is not an issue you can just handle it as a string and use the String.Replace (String, String) Method
string searchedTag = #"</contrib-group>";
string tagAndNewContent = #"</contrib-group>" + newContent;
string fileContentString = File.ReadAllText("YourFile.xml");
string ouput = fileContentString.Replace(searchedTag, tagAndNewContent);
I've got two rss feeds I would like to merge together to make one. I've actually managed to merge the two feeds together and place the items in correct place - however the data in each attribute, i.e. title contains title+link+description+author+pubDate - and that repeats for link, description, author and pubdate. Can someone help me dubug it please?
Object rssData = new object();
Cms.UI.CommonUI.ApplicationAPI AppAPI = new Cms.UI.CommonUI.ApplicationAPI();
rssData = AppAPI.ecmRssSummary(50, true, "DateCreated", 0, "");
Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.UTF8;
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(rssData.ToString());
//************************************************************
// Obtain 5 data items from second list
Object rssData1 = new object();
Cms.UI.CommonUI.ApplicationAPI AppAPI1 = new Cms.UI.CommonUI.ApplicationAPI();
rssData1 = AppAPI1.ecmRssSummary(60, true, "DateCreated", 5, "");
XmlDocument xmlDocument1 = new XmlDocument();
xmlDocument1.LoadXml(rssData1.ToString());
XmlNodeList nl = xmlDocument1.SelectNodes("/rss/channel");
XmlNode root = nl[0]; //do I need this line?
foreach (XmlNode xnode1 in root.ChildNodes)
{
string title = xnode1.InnerText;
string link = xnode1.InnerText;
string desc = xnode1.InnerText;
string auth = xnode1.InnerText;
string pdate = xnode1.InnerText;
//Merge new nodes
node = xmlDocument.CreateNode(XmlNodeType.Element, "item", null);
//node.InnerText = "this is new node";
//create title node
XmlNode nodeTitle = xmlDocument.CreateElement("title");
nodeTitle.InnerText = title;
//create Link node
XmlNode nodeLink = xmlDocument.CreateElement("link");
nodeLink.InnerText = link;
XmlNode nodeDesc = xmlDocument.CreateElement("description");
nodeDesc.InnerText = desc;
XmlNode nodeAuthor = xmlDocument.CreateElement("author");
nodeAuthor.InnerText = auth;
XmlNode nodepubDate = xmlDocument.CreateElement("pubDate");
nodepubDate.InnerText = pdate;
//add to parent node
node.AppendChild(nodeTitle);
node.AppendChild(nodeLink);
node.AppendChild(nodeDesc);
node.AppendChild(nodeAuthor);
node.AppendChild(nodepubDate);
//add to elements collection
//xmlDocument.DocumentElement.AppendChild(node);
xmlDocument.DocumentElement.SelectNodes("/rss/channel")[0].AppendChild(node);
}
//********************************************
xmlDocument.Save(Response.Output);
As you rightly suspected, the problem is here:
string title = xnode1.InnerText;
string link = xnode1.InnerText;
string desc = xnode1.InnerText;
string auth = xnode1.InnerText;
string pdate = xnode1.InnerText;
No wonder that
the data in each attribute, i.e. title contains
title+link+description+author+pubDate - and that repeats for link,
description, author and pubdate.
You need to read the values of specific subelements of each node. Probably something like:
string title = xnode1["title"].InnerText;
etc.
See http://www.csharp-examples.net/xml-nodes-by-name/ for reference.
Konrad's answer is clear, but it might not work if there's nested tags within the title tag.
Say, <title> TEXT <tag1> OTHER_TEXT </tag1></title>
On my computer, it will return you a concatenation of TEXT and OTHER_TEXT.
This should work:
string title = xnode1["title"].FirstChild.Value;
FirstChild will get you the TEXT no matter whether there is other tags with the title or not.
I've been using this code to try and get data from the google weather API, but I never get even close to pulling out what i want.
My goal is to look at:
<forecast_information>
**<city data="london uk"/>**
<postal_code data="london uk"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2011-10-09"/>
<current_date_time data="2011-10-09 12:50:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Partly Cloudy"/>
<temp_f data="68"/>
**<temp_c data="20"/>**
**<humidity data="Humidity: 68%"/>**
<icon data="/ig/images/weather/partly_cloudy.gif"/>
**<wind_condition data="Wind: W at 22 mph"/>**
</current_conditions>
And only return the text of the child nodes.
So the result would be:
City: London UK
Temp: 20c
Humidity: 68%
Wind: 22mph
Currently I am trying to use this, but got nowhere...
XmlDocument doc = new XmlDocument();
XmlNodeList _list = null;
doc.Load("http://www.google.com/ig/api?weather=london+uk");
_list = doc.GetElementsByTagName("forecast_information/");
foreach (XmlNode node in _list)
{
history.AppendText(Environment.NewLine + "City : " + node.InnerText);
}
//NOTE, currently code is set to display ALL child nodes
Perhaps someone can shed some light on the matter?
Maybe you should use node.SelectSingleNode("city").Attributes["data"].Value instead of node.InnerText
--EDIT--
This works for me
XmlDocument doc = new XmlDocument();
doc.Load("http://www.google.com/ig/api?weather=london+uk");
var list = doc.GetElementsByTagName("forecast_information");
foreach (XmlNode node in list)
{
Console.WriteLine("City : " + node.SelectSingleNode("city").Attributes["data"].Value);
}
list = doc.GetElementsByTagName("current_conditions");
foreach (XmlNode node in list)
{
foreach (XmlNode childnode in node.ChildNodes)
{
Console.Write(childnode.Attributes["data"].Value + " ");
}
}
Change that to
history.AppendText(Environment.NewLine + "City : " + node.GetAttribute("data"));
using System.Xml.Linq;
using System.Xml.XPath;
XElement doc = XElement.Load("http://www.google.com/ig/api?weather=london+uk");
string theCity = doc.XPathSelectElement(#"weather/forecast_information/city").Attribute("data").Value;
string theTemp = doc.XPathSelectElement(#"weather/current_conditions/temp_c").Attribute("data").Value;
string theHumid = doc.XPathSelectElement(#"weather/current_conditions/humidity").Attribute("data").Value;
string theWind = doc.XPathSelectElement(#"weather/current_conditions/wind_condition").Attribute("data").Value;
string resultString = String.Format("City : {0} Temp : {1}c {2} {3}", theCity, theTemp, theHumid, theWind);