C# 在C中没有从我的xmlNodes中得到任何东西#
我已经编写了一个快速代码,尝试从XML文件中获取元素并将它们放入excel表中,但我没有从节点中获取任何内容。我不明白为什么 我一直在尝试不同的方法,使用微软的XMLDocument文档从我的节点获得我需要的东西,但结果总是一样的,它会打印空行C# 在C中没有从我的xmlNodes中得到任何东西#,c#,xml-parsing,C#,Xml Parsing,我已经编写了一个快速代码,尝试从XML文件中获取元素并将它们放入excel表中,但我没有从节点中获取任何内容。我不明白为什么 我一直在尝试不同的方法,使用微软的XMLDocument文档从我的节点获得我需要的东西,但结果总是一样的,它会打印空行 [STAThread] static void Main(string[] args) { OpenFileDialog openFileDialog = new OpenFileDialog();
[STAThread]
static void Main(string[] args)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
XmlDocument xmlDocument = new XmlDocument();
System.Data.DataTable table = new System.Data.DataTable();
Microsoft.Office.Interop.Excel.Application excel;
Microsoft.Office.Interop.Excel.Workbook workBook;
Microsoft.Office.Interop.Excel.Worksheet workSheet;
int rowCount = 1;
openFileDialog.Filter = "XML Files|*.xml";
openFileDialog.Title = "Select the SMS backup";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
xmlDocument.PreserveWhitespace = true;
try
{
xmlDocument.Load(openFileDialog.FileName);
}
catch (System.IO.FileNotFoundException)
{
}
table.Columns.Add("Contact name", typeof(string));
table.Columns.Add("Date", typeof(DataFormats));
table.Columns.Add("Message", typeof(string));
foreach (XmlNode node in xmlDocument.DocumentElement.ChildNodes)
{
System.Diagnostics.Debug.WriteLine(node.InnerText);
if (node.InnerText.Contains("contact_name"))
{
table.Rows.Add(
node.Attributes["contact_name"]?.InnerText,
node.Attributes["readable_date"]?.InnerText,
node.Attributes["body"]?.InnerText
);
}
}
try
{
excel = new Microsoft.Office.Interop.Excel.Application
{
Visible = true,
DisplayAlerts = false
};
workBook = excel.Workbooks.Add(Type.Missing);
workSheet = (Microsoft.Office.Interop.Excel.Worksheet) workBook.ActiveSheet;
workSheet.Name = "SMS backup";
foreach (System.Data.DataRow dataRow in table.Rows)
{
rowCount += 1;
for (int i = 1; i <= table.Columns.Count; i++)
{
workSheet.Cells[rowCount, i] = dataRow[i - 1].ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
workSheet = null;
workBook = null;
}
}
}
[STAThread]
静态void Main(字符串[]参数)
{
OpenFileDialog OpenFileDialog=新建OpenFileDialog();
XmlDocument XmlDocument=新的XmlDocument();
System.Data.DataTable table=新的System.Data.DataTable();
Microsoft.Office.Interop.Excel.Application Excel;
Microsoft.Office.Interop.Excel.Workbook工作簿;
Microsoft.Office.Interop.Excel.Worksheet工作表;
int rowCount=1;
openFileDialog.Filter=“XML文件|*.XML”;
openFileDialog.Title=“选择SMS备份”;
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
xmlDocument.PreserveWhitespace=true;
尝试
{
加载(openFileDialog.FileName);
}
捕获(System.IO.FileNotFoundException)
{
}
表.列.添加(“联系人姓名”,类型(字符串));
表.列.添加(“日期”,类型(数据格式));
表.Columns.Add(“Message”,typeof(string));
foreach(xmlDocument.DocumentElement.ChildNodes中的XmlNode节点)
{
System.Diagnostics.Debug.WriteLine(node.InnerText);
if(node.InnerText.Contains(“contact_name”))
{
table.Rows.Add(
node.Attributes[“contact_name”]?.InnerText,
node.Attributes[“readable_date”]?.InnerText,
node.Attributes[“body”]?.InnerText
);
}
}
尝试
{
excel=新的Microsoft.Office.Interop.excel.Application
{
可见=真,
DisplayAlerts=false
};
工作簿=excel.Workbooks.Add(Type.Missing);
工作表=(Microsoft.Office.Interop.Excel.workSheet)workBook.ActiveSheet;
工作表.Name=“SMS备份”;
foreach(System.Data.DataRow表中的DataRow)
{
行计数+=1;
对于(inti=1;i后续我的评论,这里有一个更深入的建议。我是在手机上写的,所以请原谅这不是一个完整的工作代码
右键单击解决方案资源管理器中的解决方案节点,然后选择“管理解决方案的Nuget软件包”
安装epplus-它直接创建excel文件(它们实际上只是一个名为xlsx的zip文件中的xml文件),而不需要安装excel interop,这可能是一件麻烦事,最好避免
将xml文档读入数据集中:
DataSet da = new DataSet();
da.ReadXml(path to your file);
在readxml行上放置断点并启动应用程序。跨过readxml,然后指向ds
,然后单击工具提示中显示的放大镜
熟悉xml文档在数据集中的显示方式—具有子节点的节点往往表示为具有将它们链接在一起的关系的分离表,因此您可能最终会遇到类似xml的问题
xml
parentnode1
childnode1
parentnode2
childnode2
/xml
您将有两个表,称为parentnode和childnode,您将在循环集中读取它们,如:
foreach(var ro in da.Tables["parentnode"].Rows)
//do stuff with parent rows
//iterate child rows
foreach(var cro in ro.GetChildRows()){
//do stuff with child rows
}
}
现在看起来您的xml不是这样构造的,但很难说清楚
因此,您现在将xml文档作为数据集中的一组表,并希望它们作为excel工作表。Epplus只需几行代码即可从数据表(数据集是数据表的集合)创建excel文件,请参阅
如果您的数据集只包含一个表,那么作业就差不多结束了。如果它有多个表,我建议您创建一个新的DataTable,然后在数据集上循环,从各种ds.tables[“table name here”]中选择值并填充数据表
因此,要实现您的程序,请将这组注释作为代码实现
//read xml file to DataSet - 2 lines of code
//EITHER xml is flat structure, results in one datatable in the set
//so just remove or rename columns as required - few lines
//OR DataSet has 3 tables, make a new datatable and loop over the 3,
//populating the new one, to flatten it - few lines
//use epplus to turn the single datatable to an excel sheet - 4 lines
问题解决了,希望大约需要10行代码使用EPplus,将xml文件读入数据表,输出到excel。大约需要10行代码