首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么将XmlDocument转化为DataSet

2013-08-24 
如何将XmlDocument转化为DataSet?AssignmentInfo AttributeInfoAttrTypeDEPT_CODE/AttrTypeAttrT

如何将XmlDocument转化为DataSet?
<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不能实现。。
[解决办法]


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;
        }
    }
}


[解决办法]

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();

热点排行