I've searched for an answer to this, as I'm sure it will have been asked before, but can't find anything. It's quite a simple query..
I'm reading from a XML feed from a URL and can successfully loop through a node list for a top level node. However, within the Article node is a node called Categories, as an article can be associated with multiple categories. I then need to loop through
this node list within each Article. In VB, I can do this as below, but I need to convert this to C# and can't find the equivalent
VB.NET:
Dim xtr As XmlTextReader = New XmlTextReader("http://feed.url.com")
xtr.WhitespaceHandling = WhitespaceHandling.None
Dim X As New XmlDocument
X.Load(xtr)
If Not X Is Nothing Then
Try
Dim ArticleList As XmlNodeList = X.SelectNodes("InfoStreamResults/Article")
Dim Article As XmlNode
For Each Article In ArticleList
Dim CatNodesList As XmlNodeList = Article.SelectNodes("Categories/Category")
Dim category As XmlNode
For Each category In CatNodesList
' do stuff, i.e.
If Not Article.SelectSingleNode("nodename") Is Nothing Then
nodename= safeEncode(Article.SelectSingleNode("nodename").Value)
End If
Next
Next
Catch ex As Exception
' exception
End Try
End If
II can do the outer loop in C# fine, but when it comes to the equivalent of lines 17 to 28 as above, this DOESN'T work;
Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:
In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought that now we are in another foreach loop, it would become category.SelectSingleNode("category"). But I tried that
and that didn't work either.
You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.
Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:
In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought that now we are in another foreach loop, it would become category.SelectSingleNode("category"). But I tried that
and that didn't work either.
You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.
I am having a very similar problem, except it's slightly more complicated. I need to check the attribute value before deciding what to do with the inner text. I am converting a large xml file (100,000 lines) to a csv file, except i'm struggling with the
custom attributes section - see below:
What I need to do is first check for the attribute-id value, i.e.
XmlNodeList attributeList = n.SelectNodes("custom-attributes/custom-attribute");
string calendar = "null";
foreach (XmlNode a in attributeList)
{
if (a.Attributes["attribute-id"].Value == "Calendar Event")
{
calendar = a.InnerText;
}
}
And then assign it the inner value if it exists, else leave the string as null. Now all that's happening is the string is returned as nul every single time.
I am using a textwriter to write the data to a textfile, so a shortened version of the code (with everything required for this purpose is below)
Just to clarify that before we get to the custom-attributes section, the program works fine because the data is output correctly. It's only when I get to analyzing the custom-attributes section that does the data become construed.
I have tried to use a separate node list and insert the attributes that way except this doesn't have no binding or sequence then as sometimes the attributes may not exist, this is why i have to allow for them all and check them, if they are there then insert
them, if not then insert null.
Hopefully I have been clear,
Many thanks for any help in advance,
ChanandCo
Please mark as the answer if your problem has been resolved as a result of my posting - Ta
Hey, you would need to convert your XMLDocument to an XDocument/Xelement to be able to use it with LINQ.
XElement elements = XElement.Load(pathToFile);
var ranges = from r in elements.DescendantsAndSelf("range")
select r;
foreach (var x in ranges)
{
//do your work here.
}
HTH's :)
Please mark as the answer if your problem has been resolved as a result of my posting - Ta
ska_mna
Member
10 Points
39 Posts
Loop through sub XmlNodeList with C#
Apr 11, 2007 02:49 PM|LINK
I've searched for an answer to this, as I'm sure it will have been asked before, but can't find anything. It's quite a simple query..
I'm reading from a XML feed from a URL and can successfully loop through a node list for a top level node. However, within the Article node is a node called Categories, as an article can be associated with multiple categories. I then need to loop through this node list within each Article. In VB, I can do this as below, but I need to convert this to C# and can't find the equivalent
VB.NET:
Dim xtr As XmlTextReader = New XmlTextReader("http://feed.url.com") xtr.WhitespaceHandling = WhitespaceHandling.None Dim X As New XmlDocument X.Load(xtr) If Not X Is Nothing Then Try Dim ArticleList As XmlNodeList = X.SelectNodes("InfoStreamResults/Article") Dim Article As XmlNode For Each Article In ArticleList Dim CatNodesList As XmlNodeList = Article.SelectNodes("Categories/Category") Dim category As XmlNode For Each category In CatNodesList ' do stuff, i.e. If Not Article.SelectSingleNode("nodename") Is Nothing Then nodename= safeEncode(Article.SelectSingleNode("nodename").Value) End If Next Next Catch ex As Exception ' exception End Try End IfII can do the outer loop in C# fine, but when it comes to the equivalent of lines 17 to 28 as above, this DOESN'T work;
C#:
1 foreach (XmlNode category in Article.SelectNodes("Categories/Category"))
2 {
3 categoryID = category.SelectSingleNode("@ID").Value;
4 categoryName = EncodeIt(category.SelectSingleNode("category").InnerText);
5 }
So how do I loop through a set of sub nodes within a node using foreach as I do in VB.NET, but in C#?
Thanks, hope that makes sense.
Haissam
All-Star
37421 Points
5632 Posts
Re: Loop through sub XmlNodeList with C#
Apr 11, 2007 09:51 PM|LINK
Try the below code
if(X != null){
try{
XmlNodeList ArticleList = X.SelectNodes("InfoStreamResults/Article");
foreach(XmlNode Article in ArticleList){
XmlNodeList CatNodesList = Article.SelectNodes("Categories/Category");
foreach( XmlNode category in CatNodesList){
//do stuff if(Article.SelectSingleNode("nodename") != null){
nodename = safeEncode(Article.SelectSingleNode("nodename").Value);
}
}
}
}
catch{
}
}
HC
MCAD.NET
| Blog |
ska_mna
Member
10 Points
39 Posts
Re: Loop through sub XmlNodeList with C#
Apr 12, 2007 08:17 PM|LINK
Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:
XmlTextReader xtr = new XmlTextReader("http://url.feed.com"); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument X = new XmlDocument(); X.Load(xtr); if (!(X == null)) { try { XmlNodeList ArticleList = X.SelectNodes("InfoStreamResults/Article"); foreach(XmlNode Article in ArticleList) { try { XmlNodeList CatNodesList = Article.SelectNodes("Categories/Category"); foreach( XmlNode category in CatNodesList) { if (Article.SelectSingleNode("Category") != null) { categoryName = EncodeIt(Article.SelectSingleNode("Category").Value); Response.Write(categoryName); Response.Write("<br />"); } } } catch (Exception ex) { Response.Write(ex); Response.Write("<br />"); } } } catch (Exception ex) { Response.Write(ex); } }In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought that now we are in another foreach loop, it would become category.SelectSingleNode("category"). But I tried that and that didn't work either.
Incidentally, a sample of my XML looks like this;
You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.
ska_mna
Member
10 Points
39 Posts
Re: Loop through sub XmlNodeList with C#
Apr 12, 2007 08:17 PM|LINK
Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:
XmlTextReader xtr = new XmlTextReader("http://url.feed.com"); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument X = new XmlDocument(); X.Load(xtr); if (!(X == null)) { try { XmlNodeList ArticleList = X.SelectNodes("InfoStreamResults/Article"); foreach(XmlNode Article in ArticleList) { try { XmlNodeList CatNodesList = Article.SelectNodes("Categories/Category"); foreach( XmlNode category in CatNodesList) { if (Article.SelectSingleNode("Category") != null) { categoryName = EncodeIt(Article.SelectSingleNode("Category").Value); Response.Write(categoryName); Response.Write("<br />"); } } } catch (Exception ex) { Response.Write(ex); Response.Write("<br />"); } } } catch (Exception ex) { Response.Write(ex); } }In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought that now we are in another foreach loop, it would become category.SelectSingleNode("category"). But I tried that and that didn't work either.
Incidentally, a sample of my XML looks like this;
You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.
ska_mna
Member
10 Points
39 Posts
Re: Loop through sub XmlNodeList with C#
Apr 12, 2007 08:19 PM|LINK
Haissam
All-Star
37421 Points
5632 Posts
Re: Loop through sub XmlNodeList with C#
Apr 12, 2007 08:32 PM|LINK
Below is your code
if (!(X == null))
{
try
{
XmlNodeList ArticleList = X.SelectNodes("InfoStreamResults/Article");
foreach(XmlNode Article in ArticleList)
{
try
{
XmlNodeList CatNodesList = Article.SelectNodes("Categories/Category");
foreach( XmlNode category in CatNodesList)
{
if (category != null)
{
string categoryName = category.InnerText;
Response.Write(categoryName);
Response.Write("<br />");
}
}
}
catch (Exception ex)
{
Response.Write(ex);
Response.Write("<br />");
}
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
HC
-----
Mark it as answered if it helped you
MCAD.NET
| Blog |
ska_mna
Member
10 Points
39 Posts
Re: Loop through sub XmlNodeList with C#
Apr 16, 2007 10:21 AM|LINK
NathanC
Member
21 Points
18 Posts
Re: Loop through sub XmlNodeList with C#
Jul 09, 2008 10:50 AM|LINK
Hi,
I am having a very similar problem, except it's slightly more complicated. I need to check the attribute value before deciding what to do with the inner text. I am converting a large xml file (100,000 lines) to a csv file, except i'm struggling with the custom attributes section - see below:
<product product-id="048002347"> <display-name>Cotton crew-neck printed T-shirt</display-name> <long-description>Superdry cotton short-sleeved T-shirt with Osaka 6 print emblazoned across chest.</long-description> <online-flag>false</online-flag> <available-flag>true</available-flag> <searchable-flag>true</searchable-flag> <tax-class-id>default</tax-class-id> <brand>Superdry</brand> <manufacturer-name>Superdry</manufacturer-name> <custom-attributes> <custom-attribute attribute-id="Calendar Event">N/A</custom-attribute> <custom-attribute attribute-id="Care Instructions">Machine Wash 30°C</custom-attribute> <custom-attribute attribute-id="Colour">Black</custom-attribute> <custom-attribute attribute-id="Cuff Type">N/A</custom-attribute> <custom-attribute attribute-id="Exclusive">N/A</custom-attribute> <custom-attribute attribute-id="Fabric">Cotton</custom-attribute> <custom-attribute attribute-id="Fair Trade">N/A</custom-attribute> <custom-attribute attribute-id="Gender">Men</custom-attribute> <custom-attribute attribute-id="Guarantee">N/A</custom-attribute> <custom-attribute attribute-id="House of Fraser Magazine">No</custom-attribute> <custom-attribute attribute-id="Organic">No</custom-attribute> <custom-attribute attribute-id="Parent Colour">Black</custom-attribute> <custom-attribute attribute-id="Parent Style">TOPS</custom-attribute> <custom-attribute attribute-id="Range">PRE-SEASON/TRANSITIONAL</custom-attribute> <custom-attribute attribute-id="Recycled">N/A</custom-attribute> <custom-attribute attribute-id="Size">XL</custom-attribute> <custom-attribute attribute-id="Style">TOP- T-SHIRT</custom-attribute> <custom-attribute attribute-id="Television Line">No</custom-attribute> <custom-attribute attribute-id="Web Supply Lane">iForce Deliveries</custom-attribute> <custom-attribute attribute-id="qosListID">QOS1</custom-attribute> <custom-attribute attribute-id="sellByUnit">false</custom-attribute> <custom-attribute attribute-id="COMPOSITION">100% Cotton</custom-attribute> </custom-attributes> </product>What I need to do is first check for the attribute-id value, i.e.
And then assign it the inner value if it exists, else leave the string as null. Now all that's happening is the string is returned as nul every single time.
I am using a textwriter to write the data to a textfile, so a shortened version of the code (with everything required for this purpose is below)
Just to clarify that before we get to the custom-attributes section, the program works fine because the data is output correctly. It's only when I get to analyzing the custom-attributes section that does the data become construed.
I have tried to use a separate node list and insert the attributes that way except this doesn't have no binding or sequence then as sometimes the attributes may not exist, this is why i have to allow for them all and check them, if they are there then insert them, if not then insert null.
Hopefully I have been clear,
Many thanks for any help in advance,
ChanandCo
Price is what you pay; value is what get;
Anargeek77
Member
2 Points
1 Post
Re: Loop through sub XmlNodeList with C#
Sep 17, 2008 03:26 AM|LINK
Haissam over a year ago you made this one.
if (!(X == null))
{
try
{
XmlNodeList ArticleList = X.SelectNodes("InfoStreamResults/Article");
foreach(XmlNode Article in ArticleList)
{
try
{
XmlNodeList CatNodesList = Article.SelectNodes("Categories/Category");
foreach( XmlNode category in CatNodesList)
{
if (category != null)
{
string categoryName = category.InnerText;
Response.Write(categoryName);
Response.Write("<br />");
}
}
}
catch (Exception ex)
{
Response.Write(ex);
Response.Write("<br />");
}
}
}
catch (Exception ex)
{
Response.Write(ex);
}
I'm curious to know how would you accomplish same results using LINQ to XML?
-Anargeek77
NathanC
Member
21 Points
18 Posts
Re: Loop through sub XmlNodeList with C#
Sep 17, 2008 07:07 AM|LINK
Hey, you would need to convert your XMLDocument to an XDocument/Xelement to be able to use it with LINQ.
XElement elements = XElement.Load(pathToFile); var ranges = from r in elements.DescendantsAndSelf("range") select r; foreach (var x in ranges) { //do your work here. }HTH's :)Price is what you pay; value is what get;