2

I'm looking to get to value further in the XML code than I've seen examples for and with more attributes to consider.

The XML code is:

<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <database name="Members">
        <table_structure name="Logins">
            <field Field="ID" Type="int(100)" Null="NO" Key="PRI" Extra="auto_increment" Comment="" />
            <field Field="Username" Type="text" Null="YES" Key="" Extra="" Comment="" />
            <field Field="Password" Type="text" Null="YES" Key="" Extra="" Comment="" />
            <key Table="Logins" Non_unique="0" Key_name="PRIMARY" Seq_in_index="1" Column_name="ID" Collation="A" Cardinality="1" Null="" Index_type="BTREE" Comment="" />
            <options Name="Logins" Engine="MyISAM" Version="10" Row_format="Dynamic" Rows="1" Avg_row_length="20" Data_length="40" Max_data_length="281474976710655" Index_length="2048" Data_free="20" Auto_increment="3" Create_time="2011-10-14 19:30:57" Update_time="2011-10-14 19:32:21" Collation="latin1_swedish_ci" Create_options="" Comment="" />
        </table_structure>
        <table_data name="Logins">
            <row>
                <field name="ID">1</field>
                <field name="Username">MyName</field>
                <field name="Password">MyPassowrd</field>
            </row>
            <row>
                <field name="ID">2</field>
                <field name="Username">MyName2</field>
                <field name="Password">MyPassowrd2</field>
            </row>
        </table_data>
    </database>
</mysqldump>

I'm trying to get to 1, MyName and Password to then enter them into an SQL database.

The issue I'm having is getting through the multiple levels:

<database name="Members">
    <table_data name="Logins">
        <row>
            <field name="ID">

With all the examples I'm finding only ones showing 1 or 2 levels.


This seems to work,

XElement xe = XElement.Load("C:\\PATH.xml");

int count = xe.Descendants("row").Count();

var items = xe.Descendants("field").Where(n => (string)n.Attribute("name") == "ID").ToList();

var items2 = xe.Descendants("field").Where(n => (string)n.Attribute("name") == "Username").ToList();

var items3 = xe.Descendants("field").Where(n => (string)n.Attribute("name") == "Password").ToList();

             for (int i = 0; i < count; i++)
             {
                 string res = items[i].Value.ToString();
                 string res2 = items2[i].Value.ToString();
                 string res3 = items3[i].Value.ToString();
             }

I've got an ID, but I now need all its other elements in its row.

That is,

if (id = 2)
{
    string name = username.value;
    string password = password.value;
}

where name will result in myName2 and password will be MyPassword2.

I don't want to put usernames and passwords in a list.

Any advice again?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BenG
  • 14,826
  • 5
  • 45
  • 60
  • 1
    What do you mean by "getting through the multiple levels"? And can use use LINQ to XML? (It's likely to make the code much simpler.) – Jon Skeet Oct 18 '11 at 11:52
  • This person had a different problem, but in the end it was XML traversal. Look at what he did and the responses about his errors: http://stackoverflow.com/q/7806604/613130 (this if you want to use LINQ to XML) – xanatos Oct 18 '11 at 11:54
  • It isn't clear what you're asking. What have you tried? What about it doesn't work? – Tom W Oct 18 '11 at 17:04
  • Welcome to Stack Overflow! If you'd like to add additional information to your question, just edit your question (click the 'edit' link underneath your question). To comment on an answer you've received, use the comment facility under the answer. If you find the comment space insufficient, you should probably be making an edit. Answers should be just that, direct answers to your question. – Tim Post Oct 22 '11 at 17:30

1 Answers1

0

Try with LINQ to XML.

Here you have a tutorial.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frederiek
  • 1,605
  • 2
  • 17
  • 32
  • 1
    Thanks, I'll go away and look into Linq to xml. If I solve it I will post my finds for future people searching for the same problem. – BenG Oct 18 '11 at 12:16