我正在尝试将xml文件导入到c#数据网格视图中。但每当我使用.SelectNodes时,没有任何标签被读入我的xmlnodelist。每当我对代码运行调试并在它到达第一个foreach语句时逐步执行代码时它会立即离开语句,我假设因为xmlnodelist中没有任何内容。这就是我到目前为止所拥有的。请帮忙。提前致谢
namespace DesktopApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
int currRow = 0,currCell =0;
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string fileType = ofd.FileName;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(fileType);
XmlNode root = xDoc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("df", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");
XmlNodeList listOfEmployees = root.SelectNodes("descendant::Employee",nsmgr);
foreach (XmlNode employee in listOfEmployees)
{
foreach (XmlNode characteristic in employee)
{
if (characteristic.InnerText != null)
{
dataGridView1.Rows[currRow].Cells[currCell].Value = characteristic.InnerText;
}
currRow++;
currCell++;
}
}
}
}
}
}
XML:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ImportEmployees xmlns="http://m3as.com/SPDS" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<siteCode>AMH929</siteCode>
<employees>
<Employee>
<EmployeeCode>10444</EmployeeCode>
<HomeHourlyRate>11</HomeHourlyRate>
<FirstName>John|Doe</FirstName>
<LastName />
<MiddleInitial />
<Gender>M</Gender>
<DateOfBirth>20000318</DateOfBirth>
<ClockNo>10444</ClockNo>
<DateOfHire>20150318</DateOfHire>
<EffectiveDate>20160218</EffectiveDate>
<Address>0000 W MAGNOLIA STREET</Address>
<Address2 />
<City>VALDOSTA</City>
<State>GA</State>
<Country />
<PostalCode>12345</PostalCode>
<EmployeeStatus>A</EmployeeStatus>
<EmployeeType>Regular</EmployeeType>
<FullTimePartTime>FT</FullTimePartTime>
<HrlySlryNonExemptFluc1>Hourly</HrlySlryNonExemptFluc1>
<PayGroup>0101</PayGroup>
<ReviewDate />
<PhoneNumber>1234567890</PhoneNumber>
</Employee>
</employees>
</ImportEmployees>
</s:Body>
答案 0 :(得分:3)
您的命名空间声明看起来有点奇怪。这个对我有用:
var nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("spds", "http://m3as.com/SPDS");
var listOfEmployees = root.SelectNodes("//spds:Employee", nsmgr);
答案 1 :(得分:1)
尝试xml linq并在加载DGV之前将数据放入DataTable
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 WindowsFormsApplication18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(openToolStripMenuItem_Click);
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Text", typeof(string));
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string fileType = ofd.FileName;
XDocument xDoc = XDocument.Load(fileType);
Boolean first = true;
foreach (XElement employee in xDoc.Descendants().Where(x => x.Name.LocalName == "Employee"))
{
if (first)
{
foreach (XElement characteristic in employee.Elements())
{
dt.Columns.Add(characteristic.Name.LocalName, typeof(string));
}
first = false;
}
dt.Rows.Add(employee.Elements().Select(x => (string)x).ToArray());
}
dataGridView1.DataSource = dt;
}
}
}
}