按多级属性值选择XML节点值
我希望在 XML 代码中获得比我见过的示例更多的价值,并且需要考虑更多的属性。
XML 代码是:
<?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>
我正在尝试获取 1、MyName 和密码,然后将它们输入到 SQL 数据库中。
我遇到的问题是通过多个级别:
<database name="Members">
<table_data name="Logins">
<row>
<field name="ID">
在所有示例中,我发现仅显示 1 或 2 个级别。
这似乎有效,
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();
}
我有一个 ID,但我现在需要其行中的所有其他元素。
也就是说,
if (id = 2)
{
string name = username.value;
string password = password.value;
}
名称将生成 myName2,密码将生成 MyPassword2。
我不想将用户名和密码放在列表中。
还有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用LINQ to XML。
这里有 教程。
Try with LINQ to XML.
Here you have a tutorial.