如何使用C#在Xml文件中读取子节点 [英] How Do I Read Child Nodes In Xml File Using C#

查看:108
本文介绍了如何使用C#在Xml文件中读取子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我的解决方案中有一个名为XMLFile.xml的XMLFile。它包含以下数据。

 <  报告 >  
< ReportName >
每日
< / ReportName >
< StoredProcName > sp_GetDatafromTB < / StoredProcName >
< /报告 >
< 报告 >
< ReportName >
每周
< / ReportName >
< StoredProcName > sp_GetDatafromTable < / StoredProcName >
< / Report >
< 报告 >
< ReportName >
每月
< / ReportName >
< StoredProcName > sp_GetDatafromTB < / StoredProcName >
< / Report > ;
< 报告 >
< ReportName >
每年
< / ReportName >
< StoredProcName > sp_GetDatafromTable < / StoredProcName >
< span class =code-keyword>< / Report >









现在按钮点击事件我需要解析xml文件和当ReportName的名称等于下拉列表中选择的项目的名称(在UI中)并将其绑定到gridview时,获取StoredProcName的名称。我能够解析XML文件,但我如何获得可用于运行存储过程的子节点的名称。请帮助我。

解决方案

您需要先修复XML文档 - 它目前有多个根节点,这是不允许的。



要解析它,请尝试Linq to XML:

https://msdn.microsoft.com/en-us/library/bb387098.aspx [ ^ ]

XLINQ简介第3部分,共3部分 [ ^ ]

 XDocument document = XDocument.Load(  path \to\your\file.xml); 

IEnumerable< xelement> query =
来自 XElement el doc.Descendants( 报告
name =(( string )el.Element( ReportName)? ? string .Empty).Trim()
其中 string .Equals(name,theReportNameToFind,StringComparison.Ordinal)
select el;

XElement reportNode = query.First();
// 如果没有匹配的报告,则抛出异常。
< span class =code-comment> // 或者,使用FirstOrDefault并检查空返回值。

string procedureName =( string )reportNode.Element( StoredProcName);
< / xelement >


Hi,


I have an XMLFile in my solution called XMLFile.xml. It contains following data.

<Report>
    <ReportName>
      Daily
    </ReportName>
    <StoredProcName>sp_GetDatafromTB</StoredProcName>
  </Report>
  <Report>
    <ReportName>
      Weekly
    </ReportName>
    <StoredProcName>sp_GetDatafromTable</StoredProcName>
  </Report>
  <Report>
    <ReportName>
      Monthly
    </ReportName>
    <StoredProcName>sp_GetDatafromTB</StoredProcName>
  </Report>
  <Report>
    <ReportName>
      Yearly
    </ReportName>
    <StoredProcName>sp_GetDatafromTable</StoredProcName>
  </Report>





Now on button click event i need to parse the xml file and and get the name of the StoredProcName when the name of the ReportName is equal to the name of the item selected in the dropdownlist (in UI) and bind it to a gridview. I am able to parse XML file but i how do i get name of the child node which can be used to run stored procedure. Please help me.

解决方案

You'll need to start by fixing the XML document - it currently has multiple root nodes, which is not allowed.

To parse it, try Linq to XML:
https://msdn.microsoft.com/en-us/library/bb387098.aspx[^]
XLINQ Introduction Part 3 Of 3[^]

XDocument document = XDocument.Load("path\to\your\file.xml");

IEnumerable<xelement> query = 
    from XElement el in doc.Descendants("Report")
    let name = ((string)el.Element("ReportName") ?? string.Empty).Trim()
    where string.Equals(name, theReportNameToFind, StringComparison.Ordinal)
    select el;

XElement reportNode = query.First();
// This throws an exception if there are no matching reports.
// Alternatively, use "FirstOrDefault" and check for a null return value.

string procedureName = (string)reportNode.Element("StoredProcName");
</xelement>


这篇关于如何使用C#在Xml文件中读取子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆