如何将XmlDocument转化为DataSet?

蔑视的蔚蓝 2013-08-13 06:02:36
<AssignmentInfo>
<AttributeInfo>
<AttrType>DEPT_CODE</AttrType>
<AttrText>所属部门</AttrText>
<AttrValue>部门001</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>BTYPE</AttrType>
<AttrText>血型</AttrText>
<AttrValue>B型</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>SCODE</AttrType>
<AttrText>性别</AttrText>
<AttrValue>男</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>ECODE</AttrType>
<AttrText>学历</AttrText>
<AttrValue>本科</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>DECODE</AttrType>
<AttrText>学位</AttrText>
<AttrValue>学士</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>NACODE</AttrType>
<AttrText>民族</AttrText>
<AttrValue>汉族</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>PCODE</AttrType>
<AttrText>政治面貌</AttrText>
<AttrValue>群众</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>GRCODE</AttrType>
<AttrText>工人技术等级</AttrText>
<AttrValue>A级</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>JOBS_CODE</AttrType>
<AttrText>工种</AttrText>
<AttrValue>短工</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>LCCODE</AttrType>
<AttrText>用工类别</AttrText>
<AttrValue>类别001</AttrValue>
<valueDesc />
</AttributeInfo>
</AssignmentInfo>

以上为参数,,如何将这一段Xml转化为DataSet.
使用readxml不能实现。。
...全文
362 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwasdzjh 2016-03-07
  • 打赏
  • 举报
回复
引用 6 楼 u011685548 的回复:
DataSet ds = new DataSet(); var strXml = GetNewXml.InnerXml;//Xml串 XmlDocument doc = new XmlDocument(); doc.LoadXml(strXml); XmlNodeReader reader = new XmlNodeReader(doc); ds.ReadXml(reader); reader.Close(); string[] TableInfoName = new string[ds.Tables["AttributeInfo"].Rows.Count]; string[] TableInfoValue = new string[ds.Tables["AttributeInfo"].Rows.Count]; string InfoText = ""; for (int j = 0; j < ds.Tables["AttributeInfo"].Rows.Count; j++) { TableInfoName[j] = ds.Tables["AttributeInfo"].Rows[j]["AttrType"].ToString(); TableInfoValue[j] = ds.Tables["AttributeInfo"].Rows[j]["AttrValue"].ToString(); InfoText += TableInfoName[j].ToString() + ":" + TableInfoValue[j].ToString() + "\n"; } 以上是我的实现方式,谢谢为我解答的所有人!
楼主的效果图放出来呗,看看效果咋样啊
yaotomo 2013-08-14
  • 打赏
  • 举报
回复
使用ReadXml方法
终點-起點 2013-08-14
  • 打赏
  • 举报
回复
DataSet 不是有个方法ReadXmlSchema吗,直接用呗
蝶恋花雨 2013-08-14
  • 打赏
  • 举报
回复
引用 7 楼 u011685548 的回复:
谢谢你的耐心解答,还把解析XML的方式都写了出来,真的很感谢!再次感谢!
解决就结账吧。
蔑视的蔚蓝 2013-08-14
  • 打赏
  • 举报
回复
引用 1 楼 byxxw 的回复:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;

namespace Interview {
    class XmlToDataset {
        public XmlDocument loadXml() {
            var xml = @"<?xml version=""1.0"" encoding=""utf-16"" standalone=""no""?>
                            <AssignmentInfo>
                            <AttributeInfo>
                            <AttrType>DEPT_CODE</AttrType>
                            <AttrText>所属部门</AttrText>
                            <AttrValue>部门001</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>BTYPE</AttrType>
                            <AttrText>血型</AttrText>
                            <AttrValue>B型</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>SCODE</AttrType>
                            <AttrText>性别</AttrText>
                            <AttrValue>男</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>ECODE</AttrType>
                            <AttrText>学历</AttrText>
                            <AttrValue>本科</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>DECODE</AttrType>
                            <AttrText>学位</AttrText>
                            <AttrValue>学士</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>NACODE</AttrType>
                            <AttrText>民族</AttrText>
                            <AttrValue>汉族</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>PCODE</AttrType>
                            <AttrText>政治面貌</AttrText>
                            <AttrValue>群众</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>GRCODE</AttrType>
                            <AttrText>工人技术等级</AttrText>
                            <AttrValue>A级</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>JOBS_CODE</AttrType>
                            <AttrText>工种</AttrText>
                            <AttrValue>短工</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>LCCODE</AttrType>
                            <AttrText>用工类别</AttrText>
                            <AttrValue>类别001</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            </AssignmentInfo>";

            var xmldoc = new XmlDocument();
            xmldoc.LoadXml(xml);
            return xmldoc;
        }


        public DataSet ToDataSet(XmlDocument xmlDoc) {
            DataTable table = new DataTable();
            table.Columns.Add("AttrType", typeof(String));
            table.Columns.Add("AttrText", typeof(String));
            table.Columns.Add("AttrValue", typeof(String));
            table.Columns.Add("valueDesc", typeof(String));

            var nodes = xmlDoc.DocumentElement.SelectNodes("//AttributeInfo");
            foreach (XmlNode node in nodes) {
                var row = table.NewRow();
                row[0] = node.ChildNodes[0].InnerText;
                row[1] = node.ChildNodes[1].InnerText;
                row[2] = node.ChildNodes[2].InnerText;
                row[3] = node.ChildNodes[3].InnerText;
                table.Rows.Add(row);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(table);
            return ds;
        }
    }
}

谢谢你的耐心解答,还把解析XML的方式都写了出来,真的很感谢!再次感谢!
蔑视的蔚蓝 2013-08-14
  • 打赏
  • 举报
回复
DataSet ds = new DataSet(); var strXml = GetNewXml.InnerXml;//Xml串 XmlDocument doc = new XmlDocument(); doc.LoadXml(strXml); XmlNodeReader reader = new XmlNodeReader(doc); ds.ReadXml(reader); reader.Close(); string[] TableInfoName = new string[ds.Tables["AttributeInfo"].Rows.Count]; string[] TableInfoValue = new string[ds.Tables["AttributeInfo"].Rows.Count]; string InfoText = ""; for (int j = 0; j < ds.Tables["AttributeInfo"].Rows.Count; j++) { TableInfoName[j] = ds.Tables["AttributeInfo"].Rows[j]["AttrType"].ToString(); TableInfoValue[j] = ds.Tables["AttributeInfo"].Rows[j]["AttrValue"].ToString(); InfoText += TableInfoName[j].ToString() + ":" + TableInfoValue[j].ToString() + "\n"; } 以上是我的实现方式,谢谢为我解答的所有人!
蔑视的蔚蓝 2013-08-14
  • 打赏
  • 举报
回复
引用 4 楼 kongwei521 的回复:

var strXml="你的XML字符串";
XmlDocument doc = new XmlDocument();
            doc.LoadXml(strXml);
            XmlNodeReader reader = new XmlNodeReader(doc);
            DataSet ds = new DataSet();
            ds.ReadXml(reader);
            reader.Close();
            dg.DataSource = ds.Tables[0].DefaultView;
            dg.DataBind();
谢谢!我要的就是这个方法,原来要转化一次reader,而且转化方式是这样的。 再次感谢!
蝶恋花雨 2013-08-14
  • 打赏
  • 举报
回复

var strXml="你的XML字符串";
XmlDocument doc = new XmlDocument();
            doc.LoadXml(strXml);
            XmlNodeReader reader = new XmlNodeReader(doc);
            DataSet ds = new DataSet();
            ds.ReadXml(reader);
            reader.Close();
            dg.DataSource = ds.Tables[0].DefaultView;
            dg.DataBind();
ktei2008 2013-08-13
  • 打赏
  • 举报
回复
以后遇到这类问题,不需要来论坛问,直接google: C# convert XMLDocument to DataSet,一般情况下你会在StackOverflow里找到答案。
轩辕宏 2013-08-13
  • 打赏
  • 举报
回复
有个对象就是XmlreadSourc 可以读取XML文件的,你再封装到table 中就可以了啊
byxxw 2013-08-13
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;

namespace Interview {
    class XmlToDataset {
        public XmlDocument loadXml() {
            var xml = @"<?xml version=""1.0"" encoding=""utf-16"" standalone=""no""?>
                            <AssignmentInfo>
                            <AttributeInfo>
                            <AttrType>DEPT_CODE</AttrType>
                            <AttrText>所属部门</AttrText>
                            <AttrValue>部门001</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>BTYPE</AttrType>
                            <AttrText>血型</AttrText>
                            <AttrValue>B型</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>SCODE</AttrType>
                            <AttrText>性别</AttrText>
                            <AttrValue>男</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>ECODE</AttrType>
                            <AttrText>学历</AttrText>
                            <AttrValue>本科</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>DECODE</AttrType>
                            <AttrText>学位</AttrText>
                            <AttrValue>学士</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>NACODE</AttrType>
                            <AttrText>民族</AttrText>
                            <AttrValue>汉族</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>PCODE</AttrType>
                            <AttrText>政治面貌</AttrText>
                            <AttrValue>群众</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>GRCODE</AttrType>
                            <AttrText>工人技术等级</AttrText>
                            <AttrValue>A级</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>JOBS_CODE</AttrType>
                            <AttrText>工种</AttrText>
                            <AttrValue>短工</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            <AttributeInfo>
                            <AttrType>LCCODE</AttrType>
                            <AttrText>用工类别</AttrText>
                            <AttrValue>类别001</AttrValue>
                            <valueDesc />
                            </AttributeInfo>
                            </AssignmentInfo>";

            var xmldoc = new XmlDocument();
            xmldoc.LoadXml(xml);
            return xmldoc;
        }


        public DataSet ToDataSet(XmlDocument xmlDoc) {
            DataTable table = new DataTable();
            table.Columns.Add("AttrType", typeof(String));
            table.Columns.Add("AttrText", typeof(String));
            table.Columns.Add("AttrValue", typeof(String));
            table.Columns.Add("valueDesc", typeof(String));

            var nodes = xmlDoc.DocumentElement.SelectNodes("//AttributeInfo");
            foreach (XmlNode node in nodes) {
                var row = table.NewRow();
                row[0] = node.ChildNodes[0].InnerText;
                row[1] = node.ChildNodes[1].InnerText;
                row[2] = node.ChildNodes[2].InnerText;
                row[3] = node.ChildNodes[3].InnerText;
                table.Rows.Add(row);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(table);
            return ds;
        }
    }
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧