extract the blue color

  • Thread starter Thread starter Sharon
  • Start date Start date
S

Sharon

Hi guys

I don't understand what I am doing wrong

I wrote this code, in order to manipulate some Xml data, I have problem with
level 4 (properties section):



//CODE -----------------------------------------------------------------------------------------------------------------------------------------

XmlDocument xmlDoc = new XmlDocument();
XmlNodeList items = xmlDoc.GetElementsByTagName("item");
IEnumerator iEnum = items.GetEnumerator();

while(iEnum.MoveNext())
{
XmlNode itemNode = iEnum.Current ;
XmlElement name = itemNode.SelectSingleNode("name");
XmlNodeList properties =
itemNode.SelectNodes("Properties");
for(int i=0;i<properties.Count;i++)
MessageBox.Show(properties);
}

//CODE -----------------------------------------------------------------------------------------------------------------------------------------

for extracting some data on this XML file :



//XML -----------------------------------------------------------------------------------------------------------------------------------------

<items>

<item type="car">

<name ="Volvo">

<Properties>

<manufacturer="F16" />

<engine="1300" />

<color="White"/>

</properties>

</item>

</items>

//XML -----------------------------------------------------------------------------------------------------------------------------------------



The problem is that I'm getting from the for loop message : "F161300White"

Instead of 3 message boxes: "F16" "1300" "White"



please advise .

Thank you

Sharon
 
Hi Sharon,
you will need to restructure your XML, you have things like:

<engine="1300" />

this is not right, you need to either put the text inside the element i.e.
<engine>1300</engine> or put the value as an attribute value inside the
element:

<engine capacity="1300" />


Then when you loop through the properties nodes you will either say:

properties.InnerText
when using <engine>1300</engine>

or you will say properties.Attributes["capacity"].Value
when using <engine capacity="1300" />


Hope that helps
Mark R Dawson
http://www.markdawson.org
 
Thank you Mark

Sorry for wrong posting the XML file
the xml go's its like this :



Mark R. Dawson said:
Hi Sharon,
you will need to restructure your XML, you have things like:

<engine="1300" />

this is not right, you need to either put the text inside the element i.e.
<engine>1300</engine> or put the value as an attribute value inside the
element:

<engine capacity="1300" />


Then when you loop through the properties nodes you will either say:

properties.InnerText
when using <engine>1300</engine>

or you will say properties.Attributes["capacity"].Value
when using <engine capacity="1300" />


Hope that helps
Mark R Dawson
http://www.markdawson.org






Sharon said:
Hi guys

I don't understand what I am doing wrong

I wrote this code, in order to manipulate some Xml data, I have problem
with
level 4 (properties section):



//CODE -----------------------------------------------------------------------------------------------------------------------------------------

XmlDocument xmlDoc = new XmlDocument();
XmlNodeList items = xmlDoc.GetElementsByTagName("item");
IEnumerator iEnum = items.GetEnumerator();

while(iEnum.MoveNext())
{
XmlNode itemNode = iEnum.Current ;
XmlElement name = itemNode.SelectSingleNode("name");
XmlNodeList properties =
itemNode.SelectNodes("Properties");
for(int i=0;i<properties.Count;i++)
MessageBox.Show(properties);
}

//CODE -----------------------------------------------------------------------------------------------------------------------------------------

for extracting some data on this XML file :



//XML -----------------------------------------------------------------------------------------------------------------------------------------

<items>

<item type="car">

<name ="Volvo">

<Properties>

<manufacturer="F16" />

<engine="1300" />

<color="White"/>

</properties>

</item>

</items>

//XML -----------------------------------------------------------------------------------------------------------------------------------------



The problem is that I'm getting from the for loop message :
"F161300White"

Instead of 3 message boxes: "F16" "1300" "White"



please advise .

Thank you

Sharon
 
Hi Sharon,
I was not able to reproduce the issue you were seeing with the code you
submitted, I suspect that is not the exact code you are really running.
However I modified it to make it work, maybe this will help:

using System;
using System.Collections;
using System.Xml;

namespace ConsoleApplication10
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string xml = @"<items>
<item type=""car"">
<name>Volvo</name>
<properties>
<manufacturer>F16</manufacturer>
<engine>1300</engine>
<color>White</color>
</properties>
</item>
</items>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

XmlNodeList items = xmlDoc.GetElementsByTagName("item");
IEnumerator iEnum = items.GetEnumerator();

while(iEnum.MoveNext())
{
XmlNode itemNode = (XmlNode)iEnum.Current ;
XmlNode name = itemNode.SelectSingleNode("name");
XmlNodeList properties = itemNode.SelectNodes("properties");
for(int i=0;i<properties.Count;i++)
{
XmlNode propertyItem = properties.FirstChild;
while(propertyItem != null)
{
Console.WriteLine(propertyItem.InnerText);
propertyItem = propertyItem.NextSibling;
}

}
}

Console.ReadLine();
}
}
}



Sharon said:
Thank you Mark

Sorry for wrong posting the XML file
the xml go's its like this :



Mark R. Dawson said:
Hi Sharon,
you will need to restructure your XML, you have things like:

<engine="1300" />

this is not right, you need to either put the text inside the element i.e.
<engine>1300</engine> or put the value as an attribute value inside the
element:

<engine capacity="1300" />


Then when you loop through the properties nodes you will either say:

properties.InnerText
when using <engine>1300</engine>

or you will say properties.Attributes["capacity"].Value
when using <engine capacity="1300" />


Hope that helps
Mark R Dawson
http://www.markdawson.org






Sharon said:
Hi guys

I don't understand what I am doing wrong

I wrote this code, in order to manipulate some Xml data, I have problem
with
level 4 (properties section):



//CODE -----------------------------------------------------------------------------------------------------------------------------------------

XmlDocument xmlDoc = new XmlDocument();
XmlNodeList items = xmlDoc.GetElementsByTagName("item");
IEnumerator iEnum = items.GetEnumerator();

while(iEnum.MoveNext())
{
XmlNode itemNode = iEnum.Current ;
XmlElement name = itemNode.SelectSingleNode("name");
XmlNodeList properties =
itemNode.SelectNodes("Properties");
for(int i=0;i<properties.Count;i++)
MessageBox.Show(properties);
}

//CODE -----------------------------------------------------------------------------------------------------------------------------------------

for extracting some data on this XML file :



//XML -----------------------------------------------------------------------------------------------------------------------------------------

<items>

<item type="car">

<name ="Volvo">

<Properties>

<manufacturer="F16" />

<engine="1300" />

<color="White"/>

</properties>

</item>

</items>

//XML -----------------------------------------------------------------------------------------------------------------------------------------



The problem is that I'm getting from the for loop message :
"F161300White"

Instead of 3 message boxes: "F16" "1300" "White"



please advise .

Thank you

Sharon

 
Thank you Mark

This code works excellent,



I reinstalled the .Net framework (sp1 for 1.1), maybe something with the
framework was wrong ?

Because now, the previous code is working too ,

wired ...



Sharon




Mark R. Dawson said:
Hi Sharon,
I was not able to reproduce the issue you were seeing with the code you
submitted, I suspect that is not the exact code you are really running.
However I modified it to make it work, maybe this will help:

using System;
using System.Collections;
using System.Xml;

namespace ConsoleApplication10
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string xml = @"<items>
<item type=""car"">
<name>Volvo</name>
<properties>
<manufacturer>F16</manufacturer>
<engine>1300</engine>
<color>White</color>
</properties>
</item>
</items>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

XmlNodeList items = xmlDoc.GetElementsByTagName("item");
IEnumerator iEnum = items.GetEnumerator();

while(iEnum.MoveNext())
{
XmlNode itemNode = (XmlNode)iEnum.Current ;
XmlNode name = itemNode.SelectSingleNode("name");
XmlNodeList properties = itemNode.SelectNodes("properties");
for(int i=0;i<properties.Count;i++)
{
XmlNode propertyItem = properties.FirstChild;
while(propertyItem != null)
{
Console.WriteLine(propertyItem.InnerText);
propertyItem = propertyItem.NextSibling;
}

}
}

Console.ReadLine();
}
}
}



Sharon said:
Thank you Mark

Sorry for wrong posting the XML file
the xml go's its like this :

//XML -----------------------------------------------------------------------------------------------------------------------------------------

<items>

<item type="car">

<name>Volvo</name>

<Properties>

<manufacturer>F16 </manufacturer>

<engine>1300</engine>

<color>White</color>

</properties>

</item>

</items>

//XML -----------------------------------------------------------------------------------------------------------------------------------------


Mark R. Dawson said:
Hi Sharon,
you will need to restructure your XML, you have things like:

<engine="1300" />

this is not right, you need to either put the text inside the element
i.e.
<engine>1300</engine> or put the value as an attribute value inside the
element:

<engine capacity="1300" />


Then when you loop through the properties nodes you will either say:

properties.InnerText
when using <engine>1300</engine>

or you will say properties.Attributes["capacity"].Value
when using <engine capacity="1300" />


Hope that helps
Mark R Dawson
http://www.markdawson.org






:

Hi guys

I don't understand what I am doing wrong

I wrote this code, in order to manipulate some Xml data, I have
problem
with
level 4 (properties section):



//CODE -----------------------------------------------------------------------------------------------------------------------------------------

XmlDocument xmlDoc = new XmlDocument();
XmlNodeList items = xmlDoc.GetElementsByTagName("item");
IEnumerator iEnum = items.GetEnumerator();

while(iEnum.MoveNext())
{
XmlNode itemNode = iEnum.Current ;
XmlElement name =
itemNode.SelectSingleNode("name");
XmlNodeList properties =
itemNode.SelectNodes("Properties");
for(int i=0;i<properties.Count;i++)
MessageBox.Show(properties);
}

//CODE -----------------------------------------------------------------------------------------------------------------------------------------

for extracting some data on this XML file :



//XML -----------------------------------------------------------------------------------------------------------------------------------------

<items>

<item type="car">

<name ="Volvo">

<Properties>

<manufacturer="F16" />

<engine="1300" />

<color="White"/>

</properties>

</item>

</items>

//XML -----------------------------------------------------------------------------------------------------------------------------------------



The problem is that I'm getting from the for loop message :
"F161300White"

Instead of 3 message boxes: "F16" "1300" "White"



please advise .

Thank you

Sharon

 
Back
Top