我有一个像这样的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Logging S="T006" version="2" >
<Log Date="2018-11-21" Severity="Error" id="22" ID="Opened" Msg="some text"/>
<Log Date="2018-11-21" Severity="Info" id="76" ID="Auth"/>
<Log Date="2018-11-21" Severity="Info" id="60" ID="Up"/>
<Log Date="2018-11-21" Severity="Warning" id="22" ID="Opened" Msg="some text"/>
<Log Date="2018-11-21" Severity="Info" id="96" ID="Locked"/>
<Log Date="2018-11-21" Severity="Info" id="84" ID="Done"/>
<Log Date="2018-11-21" Severity="Info" id="57" ID="Idle"/>
<Log Date="2018-11-21" Severity="Error" id="10" ID="Inspected" Pos="12"/>
<Log Date="2018-11-21" Severity="Info" id="148" ID="Started"/>
</Logging>
在datagridview上显示此文件中的数据。我使用xml属性名称的名称创建表列:日期,严重性,ID,ID。
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Severity", typeof(string));
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Msg", typeof(string));
XDocument docLog = XDocument.Load(XML_FILEPATH);
foreach (XElement log in docLog.Descendants("Log"))
{
DateTime date = (DateTime)log.Attribute("Date");
string severity = (string)log.Attribute("Severity");
string id = (string)log.Attribute("id");
string ID = (string)log.Attribute("ID");
string msg = (string)log.Attribute("Msg");
dt.Rows.Add(new object[] { date, severity, id, ID, msg });
}
但是显示的数据显示“ id”列的行,并重复显示“ ID”列的行。因此它会跳过/忽略“ ID”属性的实际数据/值。
如何防止这种重复并获取“ ID”属性的值并将其显示为“ ID”列下的行?
尝试通过StreamReader在表上输出结果:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = @"All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
myLogfile = openFileDialog1.FileName;
StreamReader sr = new StreamReader(openFileDialog1.FileName);
var dataset = new DataSet();
dataset.ReadXml(sr);
var bindingSource = new BindingSource
{
DataSource = dataset,
DataMember = "Log"
};
dataGridView1.DataSource = bindingSource;
但这还会将“ id”的值复制到“ ID”中!!
答案 0 :(得分:0)
我测试了您的代码,它似乎正常工作。我在代码中发现的唯一问题是在XML文档的DateTime解析过程中,而不是:
DateTime date = (DateTime)log.Attribute("DT");
将其更改为:
DateTime date = (DateTime)log.Attribute("Date");
我在控制台应用程序上对此进行了测试。代码在这里:
using System;
using System.Data;
using System.Xml.Linq;
namespace Example
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Severity", typeof(string));
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Msg", typeof(string));
XDocument docLog = XDocument.Load("test.xml");
foreach (XElement log in docLog.Descendants("Log"))
{
DateTime date = (DateTime)log.Attribute("Date");
string severity = (string)log.Attribute("Severity");
string id = (string)log.Attribute("id");
string ID = (string)log.Attribute("ID");
string msg = (string)log.Attribute("Msg");
dt.Rows.Add(new object[] { date, severity, id, ID, msg });
}
foreach (DataRow dataRow in dt.Rows)
{
foreach (var item in dataRow.ItemArray)
{
Console.WriteLine(item);
}
}
Console.ReadLine();
}
}
}
进行此更改后的输出:
请注意,上图中此代码的输出是在调试过程中。我通过运行代码向您展示了要返回的数据表。另外,请根据您的环境更改FILEPATH。
foreach (DataRow row in dt.Rows)
{
string date = row["Date"].ToString();
string severity = row["Severity"].ToString();
string id = row["id"].ToString();
string ID = row["ID"].ToString();
string msg = row["Msg"].ToString();
}