0

我正在尝试将此 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>
4

2 回答 2

0

我会提出一种将Runner子属性的所有属性移动到Runner节点属性集合的方法。这需要以下假设:

  1. 节点中的每个嵌套元素内部最多有 1 个嵌套元素(即元素内部Runner只有一个元素)BookFixedOdds
  2. 属性将通过为其原始节点的名称添加前缀来重命名(元素中的CalcTime属性WinOdds将被复制到Runner属性的集合中,名称为WinOddsCalcTime
  3. 您可以保留或删除子节点(我在代码示例中选择删除它们)

这是代码:

    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)
    { }
        }
    }
于 2013-02-24T11:39:07.643 回答
0

您应该在每个表中都有关于 RunnerNo 的信息(WinOdd 和 PlaceOdds 中缺少它),以便您可以关联您的四个数据表。您可以将 RunnerNo 定义为 Unique

之后,您只使用一个gridview 并将四个数据表之间的关系作为gridview 的DataMember。

这是一个关系应该是什么样子的示例

于 2013-02-24T10:48:09.757 回答