Trouble reading iTunes XML feed - c#

I am trying to read an XML feed from http://itunes.apple.com/us/rss/topsongs/limit=10/genre=2/xml.
I want to access the fields like this:
<im:price amount="1.29000" currency="USD">$1.29</im:price>
<im:releaseDate label="December 31, 1960">1960-12-31T16:00:00-07:00</im:releaseDate>
Here is what I have done so far:
var xml = "http://itunes.apple.com/us/rss/topsongs/limit=10/genre=2/xml";
XmlDocument doc = new XmlDocument();
doc.Load(xml);
XmlNodeList items = doc.SelectNodes("//entry");
foreach (var item in items) {
// Do something with item.
}
No luck, though. items is null. Why? What am I doing wrong?

You need to create a namespace manager to map the RSS and also the iTunes custom tags namespace URIs to short prefixes (itunes and im in the example below):
var xml = "http://itunes.apple.com/us/rss/topsongs/limit=10/genre=2/xml";
XmlDocument doc = new XmlDocument();
doc.Load(xml);
var namespaceManager = new XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("itunes", "http://www.w3.org/2005/Atom");
namespaceManager.AddNamespace("im", "http://itunes.apple.com/rss");
XmlNodeList items = doc.SelectNodes("//itunes:entry", namespaceManager);
foreach (XmlNode item in items)
{
var price = item.SelectSingleNode("im:price", namespaceManager);
var releaseDate = item.SelectSingleNode("im:releaseDate", namespaceManager);
if (price != null)
{
Console.WriteLine(price.Attributes["amount"].InnerText);
}
if (releaseDate != null)
{
Console.WriteLine(releaseDate.Attributes["label"].InnerText);
}
}
For that specific feed you should get 10 entries.
It's in the docs as well:
If the XPath expression does not include a prefix, it is assumed that
the namespace URI is the empty namespace. If your XML includes a
default namespace, you must still use the XmlNamespaceManager and add
a prefix and namespace URI to it; otherwise, you will not get any
nodes selected. For more information, see Select Nodes Using XPath
Navigation.
Alternatively you can use a namespace-agnostic XPath (from here):
XmlNodeList items = doc.SelectNodes("//*[local-name() = 'entry']");
Finally, not sure why you said items is null. It cannot be. When running your original code you should get this:

Related

Copy Node and change Value of an Attribute

I have the following XML File. I want to copy a new "Test" and change the ID of the Test. How is it possible?
I already can copy the nodes, unfortunately not on the correct position (see images) and I also canĀ“t change the ID.
Anyone have a solution for me?
Before:
After:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Before.xml");
XmlNode Set = xmldoc.DocumentElement;
string strXmlQuery = "/Toolings/Testing/Test1";
XmlNode NodeToCopy = Set.SelectSingleNode(strXmlQuery);
XmlNode NewNode = NodeToCopy.CloneNode(true);
NodeToCopy.AppendChild(NewNode);
Set.InsertAfter(NewNode, Set.LastChild);
XPathNavigator navigator = xmldoc.CreateNavigator();
navigator.MoveToRoot();
navigator.MoveToFirstChild();
navigator.MoveToFirstChild();
navigator.MoveToFirstChild();
navigator.MoveToFirstChild();
navigator.SetValue("5678");
xmldoc.Save(After.xml");
Here is an example using System.Xml.Linq.XDocument which is a much easier API than XmlDocument:
//You can also use Load(), this is just so I didn't have to make a file
XDocument doc = XDocument.Parse("<Toolings><Testing><Test><ID>1234</ID></Test></Testing></Toolings>");
//Grab the first Test node (change the predicate if you have other search criteria)
var elTest = doc.Descendants().First(d => d.Name == "Test");
//Copy the node, only necessary if you don't know the structure at design time
XElement el = new XElement(elTest);
el.Element("ID").Value = "5678";
//inject new node
elTest.AddAfterSelf(el);
doc.Save("After.xml");

Get XmlNodeList if a particular element value or its attribute value is present in a given list of strings

I would like to get XmlNodeList from a huge XML file.
Conditions:
I have a List of unique ID values, say IDList
Case I: Collect all the nodes where element called ID has value from IDList.
Case II: Collect all nodes where one of the attribute called idName of element ID has value from IDList.
In short, extract only the nodes which match with the values given in the IDList.
I did this using some loops like load this XML to XmlDocument to iterate over all nodes and ID value but what I am looking for is some sophisticated method to do it faster and in quick way.
Because looping isn't a solution for a large XML file.
My try:
try
{
using (XmlReader reader = XmlReader.Create(URL))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNodeList nodeList = doc.GetElementsByTagName("idgroup");
foreach (XmlNode xn in nodeList)
{
string id = xn.Attributes["id"].Value;
string value = string.Empty;
if (IDList.Contains(id))
{
value = xn.ChildNodes[1].ChildNodes[1].InnerText; // <value>
if (!string.IsNullOrEmpty(value))
{
listValueCollection.Add(value);
}
}
}
}
}
catch
{}
XML (XLIFF) structure:
<XLIFF>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2">
<file date="2013-07-17">
<body>
<id idName="test_001" >
<desc-group name="test_001">
<desc type="text"/>
</desc-group>
<result-unit idName="test_001_text">
<source>abcd</source>
<result>xyz</result>
</result-unit>
</id>
</body>
</file>
</xliff>
Collect all the nodes like above where idName matches.
EDIT
This is a test that can parse the example you are giving. It attempts to reach the result node directly, so that it stays as efficient as possible.
[Test]
public void TestXPathExpression()
{
var idList = new List<string> { "test_001" };
var resultsList = new List<string>();
// Replace with appropriate method to open your URL.
using (var reader = new XmlTextReader(File.OpenRead("fixtures\\XLIFF_sample_01.xlf")))
{
var doc = new XmlDocument();
doc.Load(reader);
var root = doc.DocumentElement;
// This is necessary, since your example is namespaced.
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "urn:oasis:names:tc:xliff:document:1.2");
// Go directly to the node from which you want the result to come from.
foreach (var nodes in idList
.Select(id => root.SelectNodes("//x:file/x:body/x:id[#idName='" + id + "']/x:result-unit/x:result", nsmgr))
.Where(nodes => nodes != null && nodes.Count > 0))
resultsList.AddRange(nodes.Cast<XmlNode>().Select(node => node.InnerText));
}
// Print the resulting list.
resultsList.ForEach(Console.WriteLine);
}
You can extract only those nodes you need by using an XPath query. A brief example on how you 'd go about it:
using (XmlReader reader = XmlReader.Create(URL))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
foreach(var id in IDList) {
var nodes = doc.SelectNodes("//xliff/file/body/id[#idName='" + id + "']");
foreach(var node in nodes.Where(x => !string.IsNullOrEmpty(x.ChildNodes[1].ChildNodes[1].InnerText)))
listValueCollection.Add(node.ChildNodes[1].ChildNodes[1].InnerText);
}
}
The xpath expression is of course an example. If you want, you can post an example of your XML so I can give you something more accurate.

Parsing Field Values from Sharepoint List Services Lists.GetList

I'm trying to write something that quickly will grab field values (e.g. combo box, lookups, etc) using the Sharepoint Web Services. The following code works, but is slow and seems inefficient. Is there any way to turn this into a LINQ style query with XDocument/XElement? When I try to Parse the OuterXml it seems to load incorrectly.
MSDN - Lists.GetList
ProuductionResultNode = listservice.GetList(productiontable_listGUID);
XmlDocument doc = new XmlDocument();
doc.LoadXml(ProuductionResultNode.OuterXml);
XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
mg.AddNamespace("z", "#RowsetSchema");
mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");
XmlNodeList FieldsInList = doc.SelectNodes("//sp:Field", mg);
foreach (XmlNode Field in FieldsInList)
{
if (Field.HasChildNodes)
{
if (Field.Attributes["Name"].Value == fieldNameInternal)
{
foreach (XmlNode node in Field.ChildNodes)
{
if (node.HasChildNodes)
{
foreach (XmlNode Newnode in node.ChildNodes)
{
if (Newnode.HasChildNodes)
{
ret.Add(Newnode.InnerText);
}
}
}
}
}
}
}
return ret;
An example dropdown Field looks like this:
<Field Type="Choice" DisplayName="Media Type" Required="FALSE" Format="Dropdown" FillInChoice="FALSE" ID="{d814daf1-0bd2-48cc-8709-a513a3de4ef4}" SourceID="{1c01c034-f1fd-447f-8ed4-d60b997d0c3a}" StaticName="Media_x0020_Type" Name="Media_x0020_Type" ColName="nvarchar4" RowOrdinal="0" Version="3"><Default>CD/DVD</Default><CHOICES><CHOICE>CD/DVD</CHOICE><CHOICE>Hard Drive</CHOICE><CHOICE>Flash Drive</CHOICE><CHOICE>Virtual</CHOICE></CHOICES></Field>
The other queries I am using for GetListItems seems to work beautfiully.
XElement ziprecords = XElement.Parse(ZipItemsResultNode.OuterXml);
XName name = XName.Get("data", "urn:schemas-microsoft-com:rowset");
var iterationNotes =
from ele in ziprecords.Element(name).Elements()
where ele.Attribute("ows_Title").Value.Contains(fileName
where ele.Attribute("ows_Iteration_x0020_Notes") != null
select new { iterationNote = ele.Attribute("ows_Iteration_x0020_Notes").Value,
fileName = ele.Attribute("ows_Title").Value };
My Solution
I came up with a solution after some searching. I'm not sure why regular Parse and other methods didn't work, but this seems to fix whatever mistakes I was making. I"ll leave this here incase somebody can explain why these steps are needed. I suspect there was an issue with the encoding, which I assumed XDcoument/etc would naturally take care of.
ProuductionResultNode = listservice.GetList(productiontable_listGUID);
XmlDocument xdoc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("ans", "http://schemas.microsoft.com/sharepoint/soap/");
byte[] byteArray = Encoding.ASCII.GetBytes(ProuductionResultNode.SelectSingleNode(".//ans:Fields", nsmgr).OuterXml);
MemoryStream stream = new MemoryStream(byteArray);
XElement xe = XElement.Load(stream);
XElement qry =
(from field in xe.Descendants()
where field.Attribute("Name") != null
where field.Attribute("Name").Value == "Ship_x0020_Via"
select field).Single();
List<string> ret = new List<string>();
foreach (XElement xle in qry.XPathSelectElements(".//ans:CHOICES", nsmgr).Elements())
{
ret.Add(xle.Value);
}

Select an XML node via XPath at an arbitrary depth

Having an XML document, I want to get the first node with a certain name, no matter in which nesting depth it is contained.
I tried several things without success:
var node1 = doc.SelectSingleNode(#"//Shortcut");
var node2 = doc.SelectSingleNode(#"/*/Shortcut");
var node3 = doc.SelectSingleNode(#"//*/Shortcut");
var node4 = doc.SelectSingleNode(#"*/Shortcut");
...
Each call results in a NULL node.
I think it should be some trivial XPath syntax. Can you help me?
(In case this matters: The XML document is an input file for a WiX project, so there could be some namespace issues involved?!?).
Edit
I also tried the following:
var nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(string.Empty, #"http://schemas.microsoft.com/wix/2006/wi");
nsm.AddNamespace(#"ns", #"http://schemas.microsoft.com/wix/2006/wi");
together with:
var node1 = doc.SelectSingleNode(#"//Shortcut", nsm);
var node2 = doc.SelectSingleNode(#"/*/Shortcut", nsm);
var node3 = doc.SelectSingleNode(#"//*/Shortcut", nsm);
var node4 = doc.SelectSingleNode(#"*/Shortcut", nsm);
...
Leading to the same results.
Edit 2 - Solution
I found the solution:
var nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(string.Empty, #"http://schemas.microsoft.com/wix/2006/wi");
nsm.AddNamespace(#"ns", #"http://schemas.microsoft.com/wix/2006/wi");
and then
var node1 = doc.SelectSingleNode(#"//ns:Shortcut", nsm);
This succeeded.
The XPath expression that selects exactly the wanted node (and nothing in addition) is:
(//x:Shortcut)[1]
So, using:
doc.SelectNodes("(//x:Shortcut)[1]", someNamespaceManager)
where
the prefix "x" is bound to the namespace "http://schemas.microsoft.com/wix/2006/wi"
in someNamespaceManager
This has an advantage over the proposed solution (to use SelectSingleNode()), because it can easily be adjusted to select the N-th wanted node in the XML document.
For example:
(//x:Shortcut)[3]
selects the 3rd (in document order) x:Shortcut element, and
(//x:Shortcut)[last()]
selects the last (in document order) x:Shortcut element in the XML document.
Why do not use XDocument?
XDocument doc = XDocument.Load("test.xml");
doc.Descendants("Shortcut").First();
IMO XDocument is faster and more readable than XPath.
I finally found the solution by myself:
var nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(string.Empty, #"http://schemas.microsoft.com/wix/2006/wi");
nsm.AddNamespace(#"ns", #"http://schemas.microsoft.com/wix/2006/wi");
and then
var node1 = doc.SelectSingleNode(#"//ns:Shortcut", nsm);
This succeeded.

How to Parse XML file in c# (youtube api result)?

I'm trying to parse XML returned from the Youtue API. The APIcalls work correctly and creates an XmlDocument. I can get an XmlNodeList of the "entry" tags, but I'm not sure how to get the elements inside such as the , , etc...
XmlDocument xmlDoc = youtubeService.GetSearchResults(search.Term, "published", 1, 50);
XmlNodeList listNodes = xmlDoc.GetElementsByTagName("entry");
foreach (XmlNode node in listNodes)
{
//not sure how to get elements in here
}
The XML document schema is shown here: http://code.google.com/apis/youtube/2.0/developers_guide_protocol_understanding_video_feeds.html
I know that node.Attributes is the wrong call, but am not sure what the correct one is?
By the way, if there is a better way (faster, less memory) to do this by serializing it or using linq, I'd be happy to use that instead.
Thanks for any help!
Here some examples reading the XmlDocument. I don't know whats fastest or what needs less memory - but i would prefer Linq To Xml because of its clearness.
XmlDocument xmlDoc = youtubeService.GetSearchResults(search.Term, "published", 1, 50);
XmlNodeList listNodes = xmlDoc.GetElementsByTagName("entry");
foreach (XmlNode node in listNodes)
{
// get child nodes
foreach (XmlNode childNode in node.ChildNodes)
{
}
// get specific child nodes
XPathNavigator navigator = node.CreateNavigator();
XPathNodeIterator iterator = navigator.Select(/* xpath selector according to the elements/attributes you need */);
while (iterator.MoveNext())
{
// f.e. iterator.Current.GetAttribute(), iterator.Current.Name and iterator.Current.Value available here
}
}
and the linq to xml one:
XmlDocument xmlDoc = youtubeService.GetSearchResults(search.Term, "published", 1, 50);
XDocument xDoc = XDocument.Parse(xmlDoc.OuterXml);
var entries = from entry in xDoc.Descendants("entry")
select new
{
Id = entry.Element("id").Value,
Categories = entry.Elements("category").Select(c => c.Value)
};
foreach (var entry in entries)
{
// entry.Id and entry.Categories available here
}
I realise this has been answered and LINQ to XML is what I'd go with but another option would be XPathNavigator. Something like
XPathNavigator xmlNav = xmlDoc.CreateNavigator();
XPathNodeIterator xmlitr = xmlNav.Select("/XPath/expression/here")
while (xmlItr.MoveNext()) ...
The code is off the top of my head so it may be wrong and there may be a better way with XPathNavigator but it should give you the general idea
You could use XSD.exe to generate a class based on the schema provided. Once generated, you could then parse the XML response into the strongly typed class.
string xmlResponse = GetMyYouTubeStuff();
MyYouTubeClass response = null;
XmlHelper<MyYouTubeClass> xmlHelper = new XmlHelper<MyYouTubeClass>();
response = xmlHelper.Deserialize(xmlResponse);
And the class for deserializing it...
public class XmlHelper<T>
{
public T Deserialize(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
Byte[] byteArray = new UTF8Encoding().GetBytes(xml);
MemoryStream memoryStream = new MemoryStream(byteArray);
XmlTextReader xmlTextReader = new XmlTextReader(memoryStream);
T retObj = (T)xs.Deserialize(xmlTextReader);
return retObj;
}
}
There's also another way here.

Categories