xml数据转换为数据集
本文关键字:数据集 转换 数据 xml | 更新日期: 2023-09-27 18:00:42
我正试图将这些xml信息放入一个表中。我已经尝试将xml读取到数据集中。。。
string myXMLfile = @"..'..'..'BR7.xml";
//http://tatts.com/pagedata/racing/2011/10/5/BR7.xml
//http://tatts.com/racing/2011/10/5/BR/7
DataSet ds = new DataSet();
try
{
ds.ReadXml(myXMLfile);
for (int i = 0; i < ds.Tables.Count; i++)
{
listBox1.Items.Add(ds.Tables[i].TableName);
}
dgvRunner.DataSource = ds;
dgvRunner.DataMember = "Runner";
dgvWinOdds.DataSource = ds;
dgvWinOdds.DataMember = "WinOdds";
dgvPlaceOdds.DataSource = ds;
dgvPlaceOdds.DataMember = "PlaceOdds";
dgvFixedOdds.DataSource = ds;
dgvFixedOdds.DataMember = "FixedOdds";
但我有四张分开的桌子。Runner、WinOdds、PlaceOdds和fixedOdds如何将Runner的所有信息放到一个表中?
以下是一些xml。。。
-<Runner RunnerNo="1" Rtng="93" LastResult="0X1" Form="W" Weight="57.0" Handicap="0" Barrier="10" RiderChanged="N" Rider="P SCHMIDT(A)" Scratched="N" RunnerName="PREACHER BOY">
<WinOdds CalcTime="2011-10-05T16:51:07" LastCalcTime="2011-10-05T16:46:32" Short="N" Lastodds="11.50" Odds="10.70"/>
<PlaceOdds Short="N" Lastodds="3.50" Odds="3.30"/>
-<FixedOdds RaceDayDate="2011-10-05T00:00:00" MeetingCode="BR" RaceNo="07" RunnerNo="01" LateScratching="0" Status="w" OfferName="PREACHER BOY" RetailPlaceOdds="3.3500" RetailWinOdds="12.0000" PlaceOdds="3.3500" WinOdds="12.0000" OfferId="981020"><Book SubEventId="863449" BookStatus="F"/>
</FixedOdds>
</Runner>
您应该在每个表中都有关于RunnerNo的信息(它在WinOdd和PlaceOdds中丢失),这样您就可以关联您的四个数据表。您可以将RunnerNo定义为唯一
之后,您只使用一个gridview,并将四个数据表之间的关系作为gridview的DataMember。
下面是一个关系应该如何看起来像
我提出了一种将Runner
子属性的所有属性移动到Runner
节点属性集合的方法。这需要以下假设:
Runner
节点中的每个嵌套元素内部最多有1个嵌套元素(即FixedOdds
元素内部只有一个Book
元素)- 属性将通过在其前面加上始发节点的名称来重命名(
WinOdds
元素中的CalcTime
属性将复制到Runner
属性的集合中,名称为WinOddsCalcTime
) - 您可以保留或删除子节点(我在代码示例中选择了删除它们)
这是代码:
string myXMLfile = @"xml.xml";
DataSet ds = new DataSet();
try
{
XmlDocument doc = new XmlDocument();
doc.Load(myXMLfile);
var runners = doc.SelectNodes("/Runner");
foreach (XmlNode runner in runners)
{
foreach (XmlNode child in runner.ChildNodes)
{
for (int i = 0; i < child.Attributes.Count; i++)
{
var at =doc.CreateAttribute(child.Name + child.Attributes[i].Name);
at.Value=child.Attributes[i].Value;
runner.Attributes.Append(at);
}
if (child.Name == "FixedOdds")
{
foreach (XmlNode book in child.ChildNodes)
{
for (int i = 0; i < book.Attributes.Count; i++)
{
var at = doc.CreateAttribute(book.Name + book.Attributes[i].Name);
at.Value = book.Attributes[i].Value;
runner.Attributes.Append(at);
}
}
}
// delete the attributes and the children nodes
child.RemoveAll();
}
// delete the child noeds
while (runner.ChildNodes.Count > 0)
{
runner.RemoveChild(runner.ChildNodes[0]);
}
}
doc.Save("xml1.xml");
ds.ReadXml("xml1.xml");
for (int i = 0; i < ds.Tables.Count; i++)
{
listBox1.Items.Add(ds.Tables[i].TableName);
}
dgvRunner.DataSource = ds;
dgvRunner.DataMember = "Runner";
//dgvWinOdds.DataSource = ds;
//dgvWinOdds.DataMember = "WinOdds";
//dgvPlaceOdds.DataSource = ds;
//dgvPlaceOdds.DataMember = "PlaceOdds";
//dgvFixedOdds.DataSource = ds;
//dgvFixedOdds.DataMember = "FixedOdds";
}
catch (Exception)
{ }
}
}