我的xml的文件如下:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
- <ShellOrder>
- <OneRM>
<Sender>Shell</Sender>
<Quantity>3</Quantity>
- <Unit>
<CompName>RIY-L-99971</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v3</AdminPW>
</Unit>
- <Unit>
<CompName>PPP-L-99972</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v4</AdminPW>
</Unit>
- <Unit>
<CompName>XXX-L-99973</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v5</AdminPW>
</Unit>
</OneRM>
- <OneRM>
<Sender>Shell</Sender>
<Quantity>2</Quantity>
- <Unit>
<CompName>SLMBCA-L-99998</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>Kb9a5G2Uhp6ADx</AdminPW>
</Unit>
- <Unit>
<CompName>SDFSD-L-99998</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>Sd9a5G2Uhp6ADx</AdminPW>
</Unit>
</OneRM>
- <OneRM>
<Sender>Shell</Sender>
<Quantity>1</Quantity>
- <Unit>
<CompName>RIY-L-99974</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v3</AdminPW>
</Unit>
</OneRM>
</ShellOrder>使用dataset讀取xml,如何讀取才能完全將xml中的資料show在datagridview中?
需注意的是:同一個OneRM節點下可能會對應多個unit的節點,我需要將其全部show出來,且前面的公共部分也show出來.類似于:
-----------------------------------------------------------------
Sender Quantity CompName JoinDomain AdminPW
Shell 3 RIY-L-99971 No g6XHr8p2BCb5v3
Shell 3 RIY-L-99972 No g6XHr8p2BCb5v4
Shell 3 RIY-L-99973 No g6XHr8p2BCb5v5
Shell 2 SLMBCA-L-99998 No Kb9a5G2Uhp6ADx
Shell 2 SDFSD-L-99998 No Sd9a5G2Uhp6ADx
Shell 1 RIY-L-99974 No g6XHr8p2BCb5v3
-----------------------------------------------------------------
哪位大俠能幫個忙實現,哥們我一定將分數送上,謝謝!
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
- <ShellOrder>
- <OneRM>
<Sender>Shell</Sender>
<Quantity>3</Quantity>
- <Unit>
<CompName>RIY-L-99971</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v3</AdminPW>
</Unit>
- <Unit>
<CompName>PPP-L-99972</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v4</AdminPW>
</Unit>
- <Unit>
<CompName>XXX-L-99973</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v5</AdminPW>
</Unit>
</OneRM>
- <OneRM>
<Sender>Shell</Sender>
<Quantity>2</Quantity>
- <Unit>
<CompName>SLMBCA-L-99998</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>Kb9a5G2Uhp6ADx</AdminPW>
</Unit>
- <Unit>
<CompName>SDFSD-L-99998</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>Sd9a5G2Uhp6ADx</AdminPW>
</Unit>
</OneRM>
- <OneRM>
<Sender>Shell</Sender>
<Quantity>1</Quantity>
- <Unit>
<CompName>RIY-L-99974</CompName>
<JoinDomain>No</JoinDomain>
<AdminPW>g6XHr8p2BCb5v3</AdminPW>
</Unit>
</OneRM>
</ShellOrder>使用dataset讀取xml,如何讀取才能完全將xml中的資料show在datagridview中?
需注意的是:同一個OneRM節點下可能會對應多個unit的節點,我需要將其全部show出來,且前面的公共部分也show出來.類似于:
-----------------------------------------------------------------
Sender Quantity CompName JoinDomain AdminPW
Shell 3 RIY-L-99971 No g6XHr8p2BCb5v3
Shell 3 RIY-L-99972 No g6XHr8p2BCb5v4
Shell 3 RIY-L-99973 No g6XHr8p2BCb5v5
Shell 2 SLMBCA-L-99998 No Kb9a5G2Uhp6ADx
Shell 2 SDFSD-L-99998 No Sd9a5G2Uhp6ADx
Shell 1 RIY-L-99974 No g6XHr8p2BCb5v3
-----------------------------------------------------------------
哪位大俠能幫個忙實現,哥們我一定將分數送上,謝謝!
如果不是的话,我就来顶你一下.
string xmlFilePath="XML文件名";
StringReader StrStream = null;
XmlTextReader Xmlrdr = null;
XmlDocument xmldoc = new XmlDocument();
//根据地址加载Xml文件
xmldoc.Load(xmlFilePath);
DataSet ds = new DataSet();
//读取文件中的字符流
StrStream = new StringReader(xmldoc.InnerXml);
//获取StrStream中的数据
Xmlrdr = new XmlTextReader(StrStream);
//ds获取Xmlrdr中的数据
ds.ReadXml(Xmlrdr);
你这样读注意,可能会在dataset里面创建多个表
可是最后不能显示出我要的结果,
Unit节点下的显示不出来
1.读取XML到DataSet;
2.遍历两个表的列创建到新表的列;
3.遍历两个表的数据根OneRM_Id 添加数据;
4.绑定;
注意:两个表都有OneRM_Id所以在列的添加时要注意列的重名,行数据添加时要注意数据重复;希望对你有帮助!!
foreach(DataRow dr in ds.Tables[0].Rows)//遍历父表
{
//this.textBox1.Text = dr[0].ToString();
......
......
DataRow[] drChild = dr.GetChildRows("OneRM_Unit");//遍历子表
foreach(DataRow child in drChild)
{
//this.textBox2.Text = child[0].ToString();
......
}
}