retrieve xmlnode value in XmlDocument

  • Thread starter Thread starter David
  • Start date Start date
D

David

I have a xml file:
<OrderDetail TotalCount="2">
<ProductLine>
<LineNumber>1</LineNumber>
<ProductNumber>A001</ProductNumber>
<ProductDescription>17"VP171b</ProductDescription>
<ShipQuantity>100</ShipQuantity>
</ProductLine>
<ProductLine>
<LineNumber>2</LineNumber>
<ProductNumber>A002</ProductNumber>
<ProductDescription>17"VP171s</ProductDescription>
<ShipQuantity>200</ShipQuantity>
</ProductLine>
</OrderDetail>

And the node [ShipQuantity] xsd:integer

I want get [ShipQuantity] value in a int[] array,
How can I retrieve the 2 ProductLine's ShipQuantity value in it via C#
code?
use XmlNodeReader or XmlTextReader?

Thanks, David.
 
David,

Your schema is almost completly a dataset.
(The header is not there)
I would try to use it as that when I was you.

Something as this
\\\
DataSet ds = new DataSet();
ds.ReadXml(@"C:\Test1\david.txt");
int[] ships = new int[ds.Tables["ProductLine"].Rows.Count];
for (int i = 0;i < ds.Tables["ProductLine"].Rows.Count;i++)
{
ships = Convert.ToInt32
(ds.Tables["ProductLine"].Rows["ShipQuantity"]);
}
///
I hope this helps?

Cor
 
You could of course load the XML into an XmlDocument, iterate over all the
nodes and collect the quantities as you go. Another way is to use an XPath
query (warning: untested code follows):

string xmlText = ...;
XmlDocument xmlDoc = new xmlDoc;
xmlDoc.LoadXml(xmlText);

XmlNodeList nodes = xmlDoc.SelectNodes("//ShipQuantity");
int[] quantities = new int[nodes.Count];

for (int i = 0; i < nodes.Count; i++)
{
quantities = int.Parse(nodes.InnerText);
}

Regards,
Sami
 
Thank you, Cor.
Do you know how to transfer a XmlDocument to a DataSet?
Because my xml data is from a XLANGMessage,
and I try it like this:

XLANGPart part = msg[0];
XmlDocument doc = (XmlDocument)part.RetrieveAs(typeof(XmlDocument));
DataSet ds = new DataSet();
ds.ReadXml(doc);

But I can not put it in Dataset...
after I execute it, I got error message.

Thanks, David
 
Thank you, Sami.
But I got the error message:"Can not transfer XmlDocument to
XmlNodeList."

David
 
David,

When your xml file is as you showed, than that should do my sample, I tested
it before I have sent it.
(It was because it was in my opinion almost a dataset)

I made from your string a textfile and tested it before I have sand it.

So why you try it yourself in the same way as well.

Cor

"David" <[email protected]>

news:[email protected]...
 
I try this code, It is OK to trans dataset.

msg is a XLANGMessage:

XLANGPart part = msg[0];
XmlDocument doc = (XmlDocument)part.RetrieveAs(typeof(XmlDocument));

XmlNodeReader xnr = new XmlNodeReader(doc);
DataSet ds = new DataSet();
ds.ReadXml(xnr);

Thank you, Cor.

David.
 
Back
Top