在 C# 中的变量中加载 XML 个子节点值
Loading XML child nodes value in variables in C#
我想要 xml 个子节点值在单个变量中。根节点是“父节点”,主节点是 'Reports'。我为每个主节点写了一个循环语句 'Reports' 并提取子节点的值。
我能够在循环中加载 xml(带值),但在将子节点值分配给变量时出现以下错误。
"An unhandled exception of type 'System.NullReferenceException'
occurred in project.exe"
c#代码
public Service1()
{
InitializeComponent();
foreach (string file in Directory.EnumerateFiles(Project.Constants.PATHNAME, "*.xml"))
{
XElement ReportData = XElement.Load(file);
var Reportinfo = ReportData.Elements("Reports");
foreach (var Reports in Reportinfo.Nodes())
{
var ReportName = Reports.Document.Element("ReportName").Value;
var DBExecution = Reports.Document.Element("DBExecution").Value;
var DBName = Reports.Document.Element("DBName").Value;
}
var obj = new DatabaseAction("connection1");
var result = obj.ExecuteCommandQuery("select * from sometable");
}
}
这里是 xml:
<?xml version="1.0" encoding="utf-8"?>
<Parent>
<Reports name="Report1">
<ReportName>xzy</ReportName>
<DBExecution>Yes</DBExecution>
<DBName>sqldb0001</DBName>
<OutputFileName>xyz_output</OutputFileName>
<OutputFilePath>123</OutputFilePath>
</Reports>
<Reports name="Report2">
<ReportName>asdf</ReportName>
<DBExecution>false</DBExecution>
<DBName>sqldb0002231</DBName>
<OutputFileName>xyzasdf_output</OutputFileName>
<OutputFilePath>123333</OutputFilePath>
</Reports>
</Parent>
请告诉我哪里出错了?谢谢
这是您的解决方案。它展示了如何遍历 Reports 片段并在循环中获取各个子元素值。
c#
void Main()
{
const string file = @"e:\Temp\Manivannan.xml";
string ReportName = string.Empty;
string DBExecution = string.Empty;
string DBName = string.Empty;
XDocument ReportData = XDocument.Load(file);
foreach (var Reports in ReportData.Descendants("Reports"))
{
ReportName = Reports.Element("ReportName").Value;
DBExecution = Reports.Element("DBExecution").Value;
DBName = Reports.Element("DBName").Value;
}
}
我想要 xml 个子节点值在单个变量中。根节点是“父节点”,主节点是 'Reports'。我为每个主节点写了一个循环语句 'Reports' 并提取子节点的值。
我能够在循环中加载 xml(带值),但在将子节点值分配给变量时出现以下错误。
"An unhandled exception of type 'System.NullReferenceException' occurred in project.exe"
c#代码
public Service1()
{
InitializeComponent();
foreach (string file in Directory.EnumerateFiles(Project.Constants.PATHNAME, "*.xml"))
{
XElement ReportData = XElement.Load(file);
var Reportinfo = ReportData.Elements("Reports");
foreach (var Reports in Reportinfo.Nodes())
{
var ReportName = Reports.Document.Element("ReportName").Value;
var DBExecution = Reports.Document.Element("DBExecution").Value;
var DBName = Reports.Document.Element("DBName").Value;
}
var obj = new DatabaseAction("connection1");
var result = obj.ExecuteCommandQuery("select * from sometable");
}
}
这里是 xml:
<?xml version="1.0" encoding="utf-8"?>
<Parent>
<Reports name="Report1">
<ReportName>xzy</ReportName>
<DBExecution>Yes</DBExecution>
<DBName>sqldb0001</DBName>
<OutputFileName>xyz_output</OutputFileName>
<OutputFilePath>123</OutputFilePath>
</Reports>
<Reports name="Report2">
<ReportName>asdf</ReportName>
<DBExecution>false</DBExecution>
<DBName>sqldb0002231</DBName>
<OutputFileName>xyzasdf_output</OutputFileName>
<OutputFilePath>123333</OutputFilePath>
</Reports>
</Parent>
请告诉我哪里出错了?谢谢
这是您的解决方案。它展示了如何遍历 Reports 片段并在循环中获取各个子元素值。
c#
void Main()
{
const string file = @"e:\Temp\Manivannan.xml";
string ReportName = string.Empty;
string DBExecution = string.Empty;
string DBName = string.Empty;
XDocument ReportData = XDocument.Load(file);
foreach (var Reports in ReportData.Descendants("Reports"))
{
ReportName = Reports.Element("ReportName").Value;
DBExecution = Reports.Element("DBExecution").Value;
DBName = Reports.Element("DBName").Value;
}
}