XML如下:
<Entity>
<Code>11110</Code>
<Des>镇域范围</Des>
<EntType>LWPOLYLINE,POLYLINE</EntType>
<LayerName>04镇域范围</LayerName>
<ColorIndex />
<IsClose />
<LineType>Center</LineType>
<LineWidth />
<GlobeWidth />
<HasHatch />
<Thickness />
<Attribute ID="111100">getattr01x</Attribute>
<Ent tag="3" />
</Entity>C#点击加载的代码: DataSet set = new DataSet();
//用set读取xml文件
set.ReadXml(@"E:\\Entitys.xml");
//设置dataGridView的数据源
//DataGridView1.DataSource = set.Tables[0];
this.DataGridView1.AutoGenerateColumns = true;
this.DataGridView1.DataSource = set.Tables[0].DefaultView;
<Attribute ID="111100">getattr01x</Attribute>
<Ent tag="3" />
没有显示 应该怎么做
<Entity>
<Code>11110</Code>
<Des>镇域范围</Des>
<EntType>LWPOLYLINE,POLYLINE</EntType>
<LayerName>04镇域范围</LayerName>
<ColorIndex />
<IsClose />
<LineType>Center</LineType>
<LineWidth />
<GlobeWidth />
<HasHatch />
<Thickness />
<Attribute ID="111100">getattr01x</Attribute>
<Ent tag="3" />
</Entity>C#点击加载的代码: DataSet set = new DataSet();
//用set读取xml文件
set.ReadXml(@"E:\\Entitys.xml");
//设置dataGridView的数据源
//DataGridView1.DataSource = set.Tables[0];
this.DataGridView1.AutoGenerateColumns = true;
this.DataGridView1.DataSource = set.Tables[0].DefaultView;
<Attribute ID="111100">getattr01x</Attribute>
<Ent tag="3" />
没有显示 应该怎么做
由于每个实体都不一样
这是第二个 <Entity>
<Code>11120</Code>
<Des>镇区范围</Des>
<EntType>LWPOLYLINE,POLYLINE</EntType>
<LayerName>05镇区范围</LayerName>
<ColorIndex />
<IsClose />
<LineType>Center</LineType>
<LineWidth />
<GlobeWidth />
<HasHatch />
<Thickness />
<Attribute ID="111200">getattr01x</Attribute>
<Ent tag="3" />
</Entity>
datagridview 操作多级节点的xml文件问题
<Ent tag="3" />
应该改成
<Attribute ID="111100">getattr01x</Attribute>
<Ent>3</Ent>
我当时也想过
但是按标准这个XML是不让改的
如果不改的话 怎么做?
<Attribute ID="111200">getattr01x</Attribute>
<Ent tag="3" />
DataSet set = new DataSet();
//用set读取xml文件
set.ReadXml(@"E:\\Entitys.xml");
//设置dataGridView的数据源
//DataGridView1.DataSource = set.Tables[0];
this.DataGridView1.AutoGenerateColumns = true;
this.DataGridView1.DataSource = set.Tables[0].DefaultView;
我已经绑定了 是最后那两个节点显示的问题
专门去读取这个节点用 xPath 去定位这个节点找到值 然后在刚才加载的 DataSet里增加一个行至于更简单的方法,,目前没想到,对XML只是刚做了一些 没太深入
xmldoc.Load("a.xml");
XmlNode node = xmldoc.SelectSingleNode("");
if (node == null)
{}
foreach (XmlNode xnode in xmldoc.SelectNodes(""))
{
DataRow row = dt.NewRow();
foreach (XmlNode xcnode in xnode.ChildNodes)
{
row[xcnode.Name] = xcnode.InnerText;
}
dt.Rows.Add(row);
}
}
不太对我这样写 但是还是不行 求高手
//XmlNodeList nodes = Doc.SelectNodes("//Entitys/Entitys");
foreach (XmlNode node in nodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
XmlNode cellnode = node.ChildNodes[i];
string cellname = "Cell" + i.ToString();
if (!dataGridView1.Columns.Contains(cellname))
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.HeaderText = cellnode.Attributes["name"].Value;
column.Name = cellname;
dataGridView1.Columns.Add(column);
}
if (i == 0)
dataGridView1.Rows.Add(1);
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[cellname].Value = cellnode.InnerText;
}
}
}
this.DataGridView1.DataSource = set.Tables[0].DefaultView;
GridView1.DataBind();
能显示
只是嵌套的那两个不行
<Entity>
<Code>11110</Code>
<Des>镇域范围</Des>
<EntType>LWPOLYLINE,POLYLINE</EntType>
<LayerName>04镇域范围</LayerName>
<ColorIndex></ColorIndex>
<IsClose></IsClose>
<LineType>Center</LineType>
<LineWidth></LineWidth>
<GlobeWidth></GlobeWidth>
<HasHatch></HasHatch>
<Thickness></Thickness>
<Attribute ID="111100">getattr01x</Attribute>
<Ent tag="3"></Ent>
</Entity>
<Entity>
<Code>11120</Code>
<Des>镇区范围</Des>
<EntType>LWPOLYLINE,POLYLINE</EntType>
<LayerName>05镇区范围</LayerName>
<ColorIndex></ColorIndex>
<IsClose></IsClose>
<LineType>Center</LineType>
<LineWidth></LineWidth>
<GlobeWidth></GlobeWidth>
<HasHatch></HasHatch>
<Thickness></Thickness>
<Attribute ID="111200">getattr01x</Attribute>
<Ent tag="3"></Ent>
</Entity>
<Ent tag="3" />
应该改成
<Attribute ID="111100">getattr01x</Attribute>
<Ent>3</Ent>
Attribute 不能改