使用xmldocument c#读取xml [英] Reading an xml using xmldocument c#
问题描述
i我正在阅读以下xml,
<? xml version = 1.0 encoding = utf-8 ?>
< ExtractorData xmlns = urn:www.phas.com/services/2.3 xmlns:xsi = < span class =code-keyword> http://www.w3.org/2001/XMLSchema-instance >
< span class =code-keyword>< 级别 >
< 名称 > TestA < / Name >
< ExtractionInfo >
< 报告 ReportId = 一个 GroupId = B >
< 名称 > ReportA < /姓名 >
< OutputLocationFormat > 测试/文件夹A < / OutputLocationFormat >
< OutputFileType > PDF < / OutputFileType >
< ExecutionType >
< InstallSpecExecuteReportSaveOutput Spec FilePath = Specs \Target.xml >
< SearchPath > / content / folder [@name ='{0}'] /文件夹[@ name ='CRF报告'] < / SearchPath >
< / InstallSpecExecuteReportSaveOutput >
< / ExecutionType >
< / Report >
< / ExtractionInfo >
< /等级 >
< 级别 >
< 名称 > TestB < /姓名 >
< ExtractionInfo >
< 报告 ReportId = 一个 GroupId = B < span class =code-keyword>>
< 名称 > ReportB < /姓名 < span class =code-keyword>>
< OutputLocationFormat > 测试/ folderA < / OutputLocationFormat >
< OutputFileType > xml < / OutputFileType >
< ExecutionType >
< InstallSpecExecuteReportSaveOutput SpecFilePath = Specs\Site.xml >
< SearchPath < span class =code-keyword>> / content / folder [@name ='{0}'] / folder [@ name ='CRF Reports'] < / SearchPath >
< / InstallSpecExecuteReportSaveOutput >
< / ExecutionType >
< / Report >
< span class =code-keyword>< / ExtractionInfo >
< / Level >
< / ExtractorData >
我正在尝试使用以下代码片段获取所有级别节点
List< string> lstlevel = new List< string>();
XmlDocument document = new XmlDocument();
document.Load( D:\\Extract.config);
foreach (XmlNode节点 in document.GetElementsByTagName( 级别))
{
foreach ( node.ChildNodes中的XmlNode node1 )
{
foreach (XmlNode node2 in node.ChildNodes)
{
if (node2.Name == 名称)
{
lstlevel.Add(node2.InnerText);
}
}
}
}
lstlevel只有一个值,即,TestA但它应该包含TestA和TestB。
当我看到document.GetElementsByTagName(Level)的计数时.Count正在返回1,但它应该是2,因为我有2个Level节点。
什么'上面的代码片段错误
您正在使用XMLNode获取值,因此只有1.
您需要使用XMLNodeList。例如:
XmlNodeList elemList = doc.GetElementsByTagName( 级别);
因此,代码应首先获取所有nodeList,然后您应该在单个节点上工作,例如:
// 显示所有书名。
XmlNodeList elemList = doc.GetElementsByTagName( Level);
for ( int i = 0 ; i < elemList.Count; i ++)
{
// 使用elemList [i]
}
如下所示。
List< string> lstlevel = new List< string>();
XmlDocument document = new XmlDocument();
document.Load( D:\\Extract.config);
XmlNode root = document.FirstChild;
if (root.HasChildNodes)
{
// 获取标签名称为Level的所有节点
foreach (XmlNode node root.ChildNodes中的class =code-keyword>
{
foreach (XmlNode node1 in node.ChildNodes)
{
foreach (XmlNode node2 in node.ChildNodes)
{
if (node2.Name == 名称)
{
lstlevel.Add(node2.InnerText);
}
}
}
}
}
希望它的工作原理。
Hi,
i am reading the below xml,
<?xml version="1.0" encoding="utf-8"?>
<ExtractorData xmlns="urn:www.phas.com/services/2.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Level>
<Name>TestA</Name>
<ExtractionInfo>
<Report ReportId="one" GroupId="B">
<Name>ReportA</Name>
<OutputLocationFormat>Test/folderA</OutputLocationFormat>
<OutputFileType>PDF</OutputFileType>
<ExecutionType>
<InstallSpecExecuteReportSaveOutput SpecFilePath="Specs\Target.xml">
<SearchPath>/content/folder[@name='{0}']/folder[@name='CRF Reports']</SearchPath>
</InstallSpecExecuteReportSaveOutput>
</ExecutionType>
</Report>
</ExtractionInfo>
</Level>
<Level>
<Name>TestB</Name>
<ExtractionInfo>
<Report ReportId="one" GroupId="B">
<Name>ReportB</Name>
<OutputLocationFormat>Test/folderA</OutputLocationFormat>
<OutputFileType>xml</OutputFileType>
<ExecutionType>
<InstallSpecExecuteReportSaveOutput SpecFilePath="Specs\Site.xml">
<SearchPath>/content/folder[@name='{0}']/folder[@name='CRF Reports']</SearchPath>
</InstallSpecExecuteReportSaveOutput>
</ExecutionType>
</Report>
</ExtractionInfo>
</Level>
</ExtractorData>
I am trying to get all the Level Nodes using the below code snippet
List<string> lstlevel = new List<string>();
XmlDocument document = new XmlDocument();
document.Load("D:\\Extract.config");
foreach (XmlNode node in document.GetElementsByTagName("Level"))
{
foreach (XmlNode node1 in node.ChildNodes)
{
foreach (XmlNode node2 in node.ChildNodes)
{
if (node2.Name == "Name")
{
lstlevel.Add(node2.InnerText);
}
}
}
}
lstlevel is having only one value i.e.,TestA but it should contain Both TestA and TestB.
When i see the count of document.GetElementsByTagName("Level").Count is returining as 1 but it should be 2 as i have 2 Level Nodes.
What''s wrong in the above code snippet
You are using XMLNode to get values, thus only 1.
You need to use XMLNodeList. e.g:
XmlNodeList elemList = doc.GetElementsByTagName("Level");
So, code should first get all the nodeList and then you should work on individual nodes, like:
//Display all the book titles. XmlNodeList elemList = doc.GetElementsByTagName("Level"); for (int i=0; i < elemList.Count; i++) { // Work with elemList[i] }
Documentation related: MSDN: XmlDocument.GetElementsByTagName Method (String)[^]
Hi,
try like below.
List<string> lstlevel = new List<string>(); XmlDocument document = new XmlDocument(); document.Load("D:\\Extract.config"); XmlNode root = document.FirstChild; if(root.HasChildNodes) { // get all nodes with tag name "Level" foreach (XmlNode node in root.ChildNodes) { foreach (XmlNode node1 in node.ChildNodes) { foreach (XmlNode node2 in node.ChildNodes) { if (node2.Name == "Name") { lstlevel.Add(node2.InnerText); } } } } }
hope it works.
这篇关于使用xmldocument c#读取xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!