我编写代码是为了将从文件中读取的数据放到datagridview中。
private void button1_Click(object sender, EventArgs e)
{
XmlDataDocument xmlsach = new XmlDataDocument();
xmlsach.DataSet.ReadXml(Application.StartupPath + "\\cm_exp_20170712_221837.xml");
dataGridView1.DataSource = xmlsach.DataSet;
dataGridView1.DataMember = "ExternalUtranCell";
}
但它只显示了ExternalUtranCell的id,我想显示只放在一个表中的所有内容。我怎样才能做到这一点? 这是我的表定义的一部分。
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:un ="abc" xmlns:xn="def" xmlns:es="ghi">
<un:ExternalUtranCell id="12345-16-11">
<un:attributes>
<un:rac>11</un:rac>
<un:lac>11</un:lac>
<un:primaryCpichPower>300</un:primaryCpichPower>
<un:primaryScramblingCode>0</un:primaryScramblingCode>
<un:uarfcnDl>10588</un:uarfcnDl>
<un:uarfcnUl></un:uarfcnUl>
<un:mnc>45</un:mnc>
<un:mcc>123</un:mcc>
<un:rncId>16</un:rncId>
<un:cId>11</un:cId>
<un:userLabel></un:userLabel>
</un:attributes>
<xn:VsDataContainer id="12345-16-11">
<xn:attributes>
<xn:vsDataType>vsDataExternalUtranCell</xn:vsDataType>
<xn:vsDataFormatVersion>EricssonSpecificAttributes.16.28</xn:vsDataFormatVersion>
<es:vsDataExternalUtranCell>
<es:individualOffset>0</es:individualOffset>
<es:maxTxPowerUl>100</es:maxTxPowerUl>
<es:qQualMin>100</es:qQualMin>
<es:qRxLevMin>100</es:qRxLevMin>
<es:agpsEnabled>1</es:agpsEnabled>
<es:cellCapability>
<es:hsdschSupport>0</es:hsdschSupport>
<es:edchSupport>0</es:edchSupport>
<es:edchTti2Support>0</es:edchTti2Support>
<es:enhancedL2Support>0</es:enhancedL2Support>
<es:fdpchSupport>0</es:fdpchSupport>
<es:multiCarrierSupport>0</es:multiCarrierSupport>
<es:cpcSupport>0</es:cpcSupport>
<es:qam64MimoSupport>0</es:qam64MimoSupport>
</es:cellCapability>
<es:transmissionScheme>0</es:transmissionScheme>
<es:parentSystem>SubNetwork=ONRM_ROOT_MO_R,vsDataExternalUtranPlmn=1</es:parentSystem>
<es:mncLength>2</es:mncLength>
<es:hsAqmCongCtrlSpiSupport>1</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>2</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>3</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>4</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>7</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSupport>0</es:hsAqmCongCtrlSupport>
<es:srvccCapability>1</es:srvccCapability>
<es:reportingRange1a>0</es:reportingRange1a>
<es:reportingRange1b>0</es:reportingRange1b>
<es:timeToTrigger1a>-1</es:timeToTrigger1a>
<es:timeToTrigger1b>-1</es:timeToTrigger1b>
<es:rimCapable>0</es:rimCapable>
<es:lbUtranCellOffloadCapacity>1000</es:lbUtranCellOffloadCapacity>
</es:vsDataExternalUtranCell>
</xn:attributes>
</xn:VsDataContainer>
</un:ExternalUtranCell>
<un:ExternalUtranCell id="12345-16-11">
<un:attributes>
<un:rac>11</un:rac>
<un:lac>11</un:lac>
<un:primaryCpichPower>300</un:primaryCpichPower>
<un:primaryScramblingCode>0</un:primaryScramblingCode>
<un:uarfcnDl>10588</un:uarfcnDl>
<un:uarfcnUl></un:uarfcnUl>
<un:mnc>45</un:mnc>
<un:mcc>123</un:mcc>
<un:rncId>16</un:rncId>
<un:cId>11</un:cId>
<un:userLabel></un:userLabel>
</un:attributes>
<xn:VsDataContainer id="12345-16-11">
<xn:attributes>
<xn:vsDataType>vsDataExternalUtranCell</xn:vsDataType>
<xn:vsDataFormatVersion>EricssonSpecificAttributes.16.28</xn:vsDataFormatVersion>
<es:vsDataExternalUtranCell>
<es:individualOffset>0</es:individualOffset>
<es:maxTxPowerUl>100</es:maxTxPowerUl>
<es:qQualMin>100</es:qQualMin>
<es:qRxLevMin>100</es:qRxLevMin>
<es:agpsEnabled>1</es:agpsEnabled>
<es:cellCapability>
<es:hsdschSupport>0</es:hsdschSupport>
<es:edchSupport>0</es:edchSupport>
<es:edchTti2Support>0</es:edchTti2Support>
<es:enhancedL2Support>0</es:enhancedL2Support>
<es:fdpchSupport>0</es:fdpchSupport>
<es:multiCarrierSupport>0</es:multiCarrierSupport>
<es:cpcSupport>0</es:cpcSupport>
<es:qam64MimoSupport>0</es:qam64MimoSupport>
</es:cellCapability>
<es:transmissionScheme>0</es:transmissionScheme>
<es:parentSystem>SubNetwork=ONRM_ROOT_MO_R,vsDataExternalUtranPlmn=1</es:parentSystem>
<es:mncLength>2</es:mncLength>
<es:hsAqmCongCtrlSpiSupport>1</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>2</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>3</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>4</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSpiSupport>7</es:hsAqmCongCtrlSpiSupport>
<es:hsAqmCongCtrlSupport>0</es:hsAqmCongCtrlSupport>
<es:srvccCapability>1</es:srvccCapability>
<es:reportingRange1a>0</es:reportingRange1a>
<es:reportingRange1b>0</es:reportingRange1b>
<es:timeToTrigger1a>-1</es:timeToTrigger1a>
<es:timeToTrigger1b>-1</es:timeToTrigger1b>
<es:rimCapable>0</es:rimCapable>
<es:lbUtranCellOffloadCapacity>1000</es:lbUtranCellOffloadCapacity>
</es:vsDataExternalUtranCell>
</xn:attributes>
</xn:VsDataContainer>
</un:ExternalUtranCell>
</root>
答案 0 :(得分:0)
试试这个
XmlReader xmlFile ;
xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
dataGridView1.DataSource = ds.Tables[0]
答案 1 :(得分:0)
试试以下内容。我编辑了你的xml以获得有效的xml。您缺少名称空间:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string FILENAME = @"c:\temp\cm_exp_20170712_221837.xml";
static DataTable dt;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("rac", typeof(int));
dt.Columns.Add("lac", typeof(int));
dt.Columns.Add("primaryCpichPower", typeof(int));
dt.Columns.Add("primaryScramblingCode", typeof(int));
dt.Columns.Add("uarfcnDl", typeof(int));
dt.Columns.Add("uarfcnUl", typeof(int));
dt.Columns.Add("mnc", typeof(int));
dt.Columns.Add("mcc", typeof(int));
dt.Columns.Add("rncId", typeof(int));
dt.Columns.Add("cId", typeof(int));
dt.Columns.Add("userLabel", typeof(string));
dt.Columns.Add("vsDataType", typeof(string));
dt.Columns.Add("vsDataFormatVersion", typeof(string));
dt.Columns.Add("individualOffset", typeof(int));
dt.Columns.Add("maxTxPowerUl", typeof(int));
dt.Columns.Add("qQualMin", typeof(int));
dt.Columns.Add("qRxLevMin", typeof(int));
dt.Columns.Add("agpsEnabled", typeof(int));
dt.Columns.Add("hsdschSupport", typeof(int));
dt.Columns.Add("edchSupport", typeof(int));
dt.Columns.Add("edchTti2Support", typeof(int));
dt.Columns.Add("enhancedL2Support", typeof(int));
dt.Columns.Add("fdpchSupport", typeof(int));
dt.Columns.Add("multiCarrierSupport", typeof(int));
dt.Columns.Add("cpcSupport", typeof(int));
dt.Columns.Add("qam64MimoSupport", typeof(int));
dt.Columns.Add("transmissionScheme", typeof(int));
dt.Columns.Add("parentSystem", typeof(string));
dt.Columns.Add("mncLength", typeof(int));
dt.Columns.Add("hsAqmCongCtrlSpiSupport", typeof(string));
dt.Columns.Add("hsAqmCongCtrlSupport", typeof(int));
dt.Columns.Add("srvccCapability", typeof(int));
dt.Columns.Add("reportingRange1a", typeof(int));
dt.Columns.Add("reportingRange1b", typeof(int));
dt.Columns.Add("timeToTrigger1a", typeof(int));
dt.Columns.Add("timeToTrigger1b", typeof(int));
dt.Columns.Add("rimCapable", typeof(int));
dt.Columns.Add("lbUtranCellOffloadCapacity", typeof(int));
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace unNs = root.GetNamespaceOfPrefix("un");
XNamespace xnNs = root.GetNamespaceOfPrefix("xn");
XNamespace esNs = root.GetNamespaceOfPrefix("es");
foreach (XElement cell in doc.Descendants(unNs + "ExternalUtranCell"))
{
DataRow newRow = dt.Rows.Add();
newRow["id"] = (string)cell.Attribute("id");
XElement attributes = cell.Element(unNs + "attributes");
if (((string)attributes.Element(unNs + "rac")).Length > 0) newRow["rac"] = (int)attributes.Element(unNs + "rac");
if (((string)attributes.Element(unNs + "lac")).Length > 0) newRow["lac"] = (int)attributes.Element(unNs + "lac");
newRow["primaryCpichPower"] = (int)attributes.Element(unNs + "primaryCpichPower");
newRow["primaryScramblingCode"] = (int)attributes.Element(unNs + "primaryScramblingCode");
newRow["uarfcnDl"] = (int)attributes.Element(unNs + "uarfcnDl");
if (((string)attributes.Element(unNs + "uarfcnUl")).Length > 0) newRow["uarfcnUl"] = (int)attributes.Element(unNs + "uarfcnUl");
newRow["mnc"] = (int)attributes.Element(unNs + "mnc");
newRow["mcc"] = (int)attributes.Element(unNs + "mcc");
newRow["rncId"] = (int)attributes.Element(unNs + "rncId");
newRow["cId"] = (int)attributes.Element(unNs + "cId");
newRow["userLabel"] = (string)attributes.Element(unNs + "userLabel");
XElement vsDataContainer = cell.Element(xnNs + "VsDataContainer");
XElement vsDataContainerAttributes = vsDataContainer.Element(xnNs + "attributes");
newRow["vsDataType"] = (string)vsDataContainerAttributes.Element(xnNs + "vsDataType");
newRow["vsDataFormatVersion"] = (string)vsDataContainerAttributes.Element(xnNs + "vsDataFormatVersion");
XElement vsDataExternalUtranCell = vsDataContainerAttributes.Element(esNs + "vsDataExternalUtranCell");
newRow["individualOffset"] = (int)vsDataExternalUtranCell.Element(esNs + "individualOffset");
newRow["maxTxPowerUl"] = (int)vsDataExternalUtranCell.Element(esNs + "maxTxPowerUl");
newRow["qQualMin"] = (int)vsDataExternalUtranCell.Element(esNs + "qQualMin");
newRow["qRxLevMin"] = (int)vsDataExternalUtranCell.Element(esNs + "qRxLevMin");
newRow["agpsEnabled"] = (int)vsDataExternalUtranCell.Element(esNs + "agpsEnabled");
XElement cellCapability = vsDataExternalUtranCell.Element(esNs + "cellCapability");
newRow["hsdschSupport"] = (int)cellCapability.Element(esNs + "hsdschSupport");
newRow["edchSupport"] = (int)cellCapability.Element(esNs + "edchSupport");
newRow["edchTti2Support"] = (int)cellCapability.Element(esNs + "edchTti2Support");
newRow["enhancedL2Support"] = (int)cellCapability.Element(esNs + "enhancedL2Support");
newRow["fdpchSupport"] = (int)cellCapability.Element(esNs + "fdpchSupport");
newRow["multiCarrierSupport"] = (int)cellCapability.Element(esNs + "multiCarrierSupport");
newRow["cpcSupport"] = (int)cellCapability.Element(esNs + "cpcSupport");
newRow["qam64MimoSupport"] = (int)cellCapability.Element(esNs + "qam64MimoSupport");
newRow["transmissionScheme"] = (int)vsDataExternalUtranCell.Element(esNs + "transmissionScheme");
newRow["parentSystem"] = (string)vsDataExternalUtranCell.Element(esNs + "parentSystem");
newRow["mncLength"] = (string)vsDataExternalUtranCell.Element(esNs + "mncLength");
newRow["hsAqmCongCtrlSpiSupport"] = string.Join(",", vsDataExternalUtranCell.Elements(esNs + "hsAqmCongCtrlSpiSupport").Select(x => (string)x));
newRow["hsAqmCongCtrlSupport"] = (int)vsDataExternalUtranCell.Element(esNs + "hsAqmCongCtrlSupport");
newRow["srvccCapability"] = (int)vsDataExternalUtranCell.Element(esNs + "srvccCapability");
newRow["reportingRange1a"] = (int)vsDataExternalUtranCell.Element(esNs + "reportingRange1a");
newRow["reportingRange1b"] = (int)vsDataExternalUtranCell.Element(esNs + "reportingRange1b");
newRow["timeToTrigger1a"] = (int)vsDataExternalUtranCell.Element(esNs + "timeToTrigger1a");
newRow["timeToTrigger1b"] = (int)vsDataExternalUtranCell.Element(esNs + "timeToTrigger1b");
newRow["rimCapable"] = (int)vsDataExternalUtranCell.Element(esNs + "rimCapable");
newRow["lbUtranCellOffloadCapacity"] = (int)vsDataExternalUtranCell.Element(esNs + "lbUtranCellOffloadCapacity");
newRow["agpsEnabled"] = (int)vsDataExternalUtranCell.Element(esNs + "agpsEnabled");
}
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
更新了代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
const string FILENAME = @"C:\temp\cm_exp_20170714_013357.xml";
static DataTable dt;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("vsDataType", typeof(string));
dt.Columns.Add("vsDataFormatVersion", typeof(string));
dt.Columns.Add("localCellId", typeof(int));
dt.Columns.Add("physicalLayerCellIdGroup", typeof(int));
dt.Columns.Add("physicalLayerSubCellId", typeof(int));
dt.Columns.Add("userLabel", typeof(int));
dt.Columns.Add("tac", typeof(int));
dt.Columns.Add("pciCell", typeof(string));
//dt.Columns.Add("mcc", typeof(int));
//dt.Columns.Add("mnc", typeof(int));
//dt.Columns.Add("mncLength", typeof(int));
//dt.Columns.Add("pciConflict", typeof(string));
//dt.Columns.Add("enbId", typeof(int));
//dt.Columns.Add("cellId", typeof(int));
dt.Columns.Add("pciCellmcc", typeof(int));
//dt.Columns.Add("pciConflictCellmnc", typeof(int));
//dt.Columns.Add("pciConflictCellmncLength", typeof(int));
dt.Columns.Add("earfcndl", typeof(int));
dt.Columns.Add("lbEUtranCellOffloadCapacity", typeof(int));
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace xnNs = root.GetNamespaceOfPrefix("xn");
XNamespace esNs = root.GetNamespaceOfPrefix("es");
DataRow newRow = null;
XElement attributes = null;
foreach (XElement cell in doc.Descendants(xnNs + "VsDataContainer"))
{
attributes = cell.Element(xnNs + "attributes");
XElement vsDataExternalEUtranCellFDD = attributes.Element(esNs + "vsDataExternalEUtranCellFDD");
if (vsDataExternalEUtranCellFDD != null)
{
List<KeyValuePair<string, XElement>> pciCells = vsDataExternalEUtranCellFDD.Elements(esNs + "pciConflictCell")
.Select(x => new KeyValuePair<string,XElement>("conflictCell", x)).ToList();
pciCells.AddRange(vsDataExternalEUtranCellFDD.Elements(esNs + "pciDetectingCell")
.Select(x => new KeyValuePair<string, XElement>("detectingCell", x)).ToList());
foreach (KeyValuePair<string, XElement> pciCell in pciCells)
{
newRow = dt.Rows.Add();
newRow["id"] = (string)cell.Attribute("id");
newRow["vsDataType"] = (string)attributes.Element(xnNs + "vsDataType");
newRow["vsDataFormatVersion"] = (string)attributes.Element(xnNs + "vsDataFormatVersion");
newRow["localCellId"] = (int)vsDataExternalEUtranCellFDD.Element(esNs + "localCellId");
newRow["physicalLayerCellIdGroup"] = (int)vsDataExternalEUtranCellFDD.Element(esNs + "physicalLayerCellIdGroup");
newRow["physicalLayerSubCellId"] = (int)vsDataExternalEUtranCellFDD.Element(esNs + "physicalLayerSubCellId");
newRow["tac"] = (int)vsDataExternalEUtranCellFDD.Element(esNs + "tac");
//XElement activePlmnList = vsDataExternalEUtranCellFDD.Element(esNs + "activePlmnList");
// newRow["mcc"] = (int)activePlmnList.Element(esNs + "mcc");
// newRow["mnc"] = (int)activePlmnList.Element(esNs + "mnc");
// newRow["mncLength"] = (int)activePlmnList.Element(esNs + "mncLength");
// if (((string)vsDataExternalEUtranCellFDD.Element(esNs + "pciConflict")).Length > 0) newRow["pciConflict"] = string.Join(",", vsDataExternalEUtranCellFDD.Elements(esNs + "pciConflict").Select(x => (string)x));
if (pciCell.Value != null)
{
if ((pciCell.Value.Element(esNs + "mcc") != null) && ((string)pciCell.Value.Element(esNs + "mcc")).Length > 0)
{
newRow["pciCell"] = pciCell.Key;
newRow["pciCellmcc"] = (int)pciCell.Value.Element(esNs + "mcc");
}
}
newRow["earfcndl"] = (int)vsDataExternalEUtranCellFDD.Element(esNs + "earfcndl");
newRow["lbEUtranCellOffloadCapacity"] = (int)vsDataExternalEUtranCellFDD.Element(esNs + "lbEUtranCellOffloadCapacity");
}
}
else
{
newRow = dt.Rows.Add();
newRow["id"] = (string)cell.Attribute("id");
attributes = cell.Element(xnNs + "attributes");
newRow["vsDataType"] = (string)attributes.Element(xnNs + "vsDataType");
newRow["vsDataFormatVersion"] = (string)attributes.Element(xnNs + "vsDataFormatVersion");
}
}
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}