0

I have an xml file that I have to read it and extract values from nodes in to some variables, I came across a node that I don't know how to extract data, this is the node:

<QuantityInIssueUnit uom="KO">288.000</QuantityInIssueUnit> 

So i have to extract KO and 288.00 and pass it to variables, i tried this:

if (!dr_art_line.Table.Columns.Contains("QuantityInIssueUnit") || 

    dr_art_line["QuantityInIssueUnit"].ToString().Length <= 0)
                                {
                                    QuantityInIssueUnit = 0;
                                }
                                else
                                {
                                QuantityInIssueUnit = Convert.ToDecimal(dr_art_line["QuantityInIssueUnit"]);
                                 {

and this:

if (!dr_art_line.Table.Columns.Contains("QuantityInIssueUnit uom") || dr_art_line["QuantityInIssueUnit uom"].ToString().Length <= 0)
                            {
                                QuantityInIssueUnit_uom = 0;
                            }
                            else
                            {
                                QuantityInIssueUnit_uom = Convert.ToDecimal(dr_art_line["QuantityInIssueUnit uom"]);
                            }

But every time the QuantityInIssueUnit is 0, I know I'm doing somethig wrong in reading, what is the propper way to read this kind of node?

Thanks!

Habib
  • 219,104
  • 29
  • 407
  • 436
CrBruno
  • 963
  • 8
  • 18
  • 33
  • 2
    Sorry, I may be ignorant about a way of interacting with XML that I'm not familiar with, but why are you interacting with XML as a table here? Why not use an `XmlDocument` or an `XmlReader`? – Brian Warshaw Jun 12 '12 at 11:26
  • Dupe of http://stackoverflow.com/questions/904748/how-can-i-read-an-xml-attribute-using-readxml-how-does-dataset-readxml-translat ? – Mike Miller Jun 12 '12 at 11:29

1 Answers1

0

DOCUMENT.xml

<ROOT>
    < . . . >
    <QuantityInIssueUnit uom="KO">288.000</QuantityInIssueUnit>
    < . . . >
</ROOT>

If this is your setup, I would use something like:

public static float ReadFromXml(string f, string n)
{
    string quantityInIssueUnit;
    XmlReader reader = XmlReader.Create(f);
    reader.ReadToFollowing(n);
    quantityInIssueUnit = reader.ReadInnerXml( );
    reader.Close( );
    return float.Parse(quantityInIssueUnit);
}

So you could call:

ReadFromXml(@"C:\\...\DOCUMENT.xml", "QuantityInIssueUnit");
// Returns 288 as a float
CM90
  • 825
  • 1
  • 7
  • 9