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
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;