标题:XML的问题,请高手解答
只看楼主
kristytian
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-7-10
 问题点数:0 回复次数:5 
XML的问题,请高手解答
<PointRecord ID="00000057" TimeStamp="2010-07-05T09:39:59">
            <Name>Yzc</Name>
            <Code/>
            <Method>GpsRTKControlPoint</Method>
            <SurveyMethod>Fix</SurveyMethod>
            <Classification>Normal</Classification>
            <Deleted>false</Deleted>

            <ECEFDeltas>
                <DeltaX>1838.5519045149</DeltaX>
                <DeltaY>482.8904503677</DeltaY>
                <DeltaZ>-333.16447392013</DeltaZ>
            </ECEFDeltas>
            <ReferenceRecordID>0000004d</ReferenceRecordID>
            <RTK_Base>A1</RTK_Base>
            <AntennaID>00000056</AntennaID>

            <ComputedGrid>
                <North>33935.771457227</North>
                <East>74407.768982798</East>
                <Elevation>567.1</Elevation>
            </ComputedGrid>
            <Precision>
                <Horizontal>0.00616929062489</Horizontal>
                <Vertical>0.01237836094254</Vertical>
            </Precision>
            <QualityControl1>
                <NumberOfSatellites>12</NumberOfSatellites>
                <RelativeDOPs>true</RelativeDOPs>
                <PDOP>1.8156530857086</PDOP>
                <HDOP>0.8749361038208</HDOP>
                <VDOP>1.6248326301575</VDOP>
                <RMS>16.82648435235</RMS>
                <NumberOfPositionsUsed>32</NumberOfPositionsUsed>
                <HorizontalStandardDeviation/>
                <VerticalStandardDeviation/>
                <StartTime>
                    <GPSWeek>1591</GPSWeek>
                    <Seconds>92384.2</Seconds>
                </StartTime>
                <EndTime>
                    <GPSWeek>1591</GPSWeek>
                    <Seconds>92415.2</Seconds>
                </EndTime>
                <MonitorStatus>NotMonitored</MonitorStatus>
            </QualityControl1>
        </PointRecord>
请教如何提取PointRecord中的需要的字段在dataGridView表中显示,比如说我需要显示的是Name+North+East+Elevation+……
简单的代码如下:
            DataSet myDataSet = new DataSet();
            myDataSet.ReadXml("20100705.xml");
            dataGridView1.DataSource = myDataSet.Tables["PointRecord"];
请高手帮忙,谢谢
搜索更多相关主题的帖子: false 
2011-10-03 11:22
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 

程序代码:
            DataSet myDataSet = new DataSet();
            myDataSet.ReadXml("20100705.xml");
            //dataGridView1.DataSource = myDataSet.Tables["PointRecord"]; 注释该句

            //定义需要显示的列头数组
            string[] aryColums = new string[] { "Name", "North", "East", "Elevation" };
            //定义需要显示的行数据的数组
            string[] aryData = null;
            //用数组初始化列头
            for (int i = 0; i < aryColums.Length; i++)
            {
                dataGridView1.Columns.Add(aryColums[i], aryColums[i]);
            }

            //取PointRecord表数据(Name在该表中)
            DataTable t_PointRecord = myDataSet.Tables["PointRecord"];
            //取ComputedGrid表数据(Notrh,Ease,Elevation在该表中)
            DataTable t_ComputedGrid = myDataSet.Tables["ComputedGrid"];
            //定义关联ID(考虑xml中可能有多个PointRecord节点,id用于关联子节点下面的数据,即当前dataSet中表ComputedGrid的数据)
            string id = "";

            //清空dataGridView
            dataGridView1.Rows.Clear();

            //xml中有多少个PointRecord节点,PointRecord表中就有多少条数据,以PointRecord_Id标识唯一性
            for (int i = 0; i < t_PointRecord.Rows.Count; i++)
            { 

                //创建行数据数组(上面只是定义,这里才new)
                aryData = new string[aryColums.Length];
                //获取到PointRecord_Id值,用于查询ComputedGrid表数据
                id = t_PointRecord.Rows[i]["PointRecord_Id"].ToString();
                //从PointRecord表中取Name
                //注意aryData数组中值的顺序,该顺序为显示顺序
                aryData[0] = t_PointRecord.Rows[i]["Name"].ToString();
                //从ComputedGrid表中取North
                //首先查询PointRecord_Id = id 的记录,Select方法返回的是行的数组,因为满足条件的行可能存在多行
                //但就我们的情况,PointRecord节点下面有且只有一个ComputedGrid节点,所以直接取数组最上面一维,即[0]
                //取到行对象后,再使用Row[列名]取值,再ToString后存放到aryData数组中
                aryData[1] = t_ComputedGrid.Select("PointRecord_Id = '" + id + "'")[0]["North"].ToString();
                //从ComputedGrid表中取East
                aryData[2] = t_ComputedGrid.Select("PointRecord_Id = '" + id + "'")[0]["East"].ToString();
                //从ComputedGrid表中取Elevation
                aryData[3] = t_ComputedGrid.Select("PointRecord_Id = '" + id + "'")[0]["Elevation"].ToString();
                //使用aryData构到Row对象并添加到dataGridView中
                dataGridView1.Rows.Add(aryData);
楼主国庆都还在学习,真用心

2011-10-03 14:02
kristytian
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-7-10
得分:0 
非常感谢,看了下,只有一个PointRecord根节点是没有问题的,但是如果同一个XML文件中有很多个PointRecord节点的时候,就有问题了
2011-10-03 16:02
kristytian
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-7-10
得分:0 
非常感谢yhlvht的回复,祝大家国庆愉快
2011-10-03 16:03
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 
xml中怎么可能会有多个相同的根节点,这是不可能的。
我说的情况是PointRecord不作为根节点,是子节点的情况
如果PointRecord是作为根节点的,那dataTable中只可能有一条记录,直接取字段名就可以了,也不用循环什么的了
2011-10-03 18:02
rongjia_2011
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-9-20
得分:0 
   路过 学习中 以后会用到  
2011-10-06 14:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-351500-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.103167 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved