C# 读取带xmlns 命名空间的XML

来源:互联网 发布:saas模式 数据库设计 编辑:程序博客网 时间:2024/05/24 06:31
  1.  private void ViewTask1(XmlDocument root)
  2.     {
  3.         //验证略
  4.         string strXPath = "//abc:SummaryName";
  5.         XmlNamespaceManager namespaceManager = new XmlNamespaceManager(root.NameTable); //namespace
  6.         namespaceManager.AddNamespace("abc""http://tempuri.org/");
  7.         XmlNodeList list = root.SelectNodes(strXPath, namespaceManager);
  8.         DataTable dt = new DataTable();
  9.         if (root.SelectNodes(strXPath, namespaceManager).Count == 0)
  10.         {
  11.             this.GridView1.EmptyDataText = "没有带办项目";
  12.             this.GridView1.DataBind();
  13.         }
  14.         else
  15.         {
  16.             //给dt添加列
  17.             XmlNode xmlnode = root.SelectSingleNode(strXPath, namespaceManager).ChildNodes[0];
  18.             DataColumn dc = null;
  19.             for (int i = 0; i < xmlnode.Attributes.Count; i++)
  20.             {
  21.                 dc = new DataColumn(xmlnode.Attributes[i].Name);
  22.                 dt.Columns.Add(dc);
  23.             }
  24.             foreach (XmlNode n in list)
  25.             {
  26.                 foreach (XmlNode nn in n.ChildNodes)
  27.                 {
  28.                     DataRow dr = dt.NewRow();
  29.                     for (int i = 0; i < dt.Columns.Count; i++)
  30.                     {
  31.                         dr[i] = nn.Attributes[i].Value;
  32.                     }
  33.                     dt.Rows.Add(dr);
  34.                 }
  35.             }
  36.         }
  37.         this.GridView1.DataSource = dt;
  38.         this.GridView1.DataBind();
  39.     }
原创粉丝点击