简体   繁体   English

使用属性从XML读取数据

[英]Reading data from XML with attributes

I have an XML file which is like below: 我有一个XML文件,如下所示:

<CPageDataXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<control id="busRowOAppr2EIDLookUpUserControl" controltype="business">
    <field controlvaluetype="single" key="busRowOAppr2EIDLookUpUserControl_txtEID">
      <valuefield value="709227">E8 - John Doe</valuefield>
    </field>
    <field controlvaluetype="hidden_single" key="busRowOAppr2EIDLookUpUserControl_txtEID_Email">
      <valuefield value="_JohnDoe@Wonder.com">emailid</valuefield>
    </field>
</control>
<control id="busDelegationFromDate123" controltype="business">
    <field controlvaluetype="single" key="txtCalanderDateWithImage_UserControl">
      <valuefield value="" />
    </field>
</control>
</CPageDataXML>

I want to read the value of the valuefield where control id="busRowOAppr2EIDLookUpUserControl" 我想读取值字段的值,其中控件id =“ busRowOAppr2EIDLookUpUserControl”

The C# code is: This is the code for loading the XML: C#代码是:这是用于加载XML的代码:

XmlDocument xPagedata=new XmlDocument();
XmlNode xnodePagedata = null;
xPagedata.LoadXml(strPageData);

This is the code for SelectSingleNode: 这是SelectSingleNode的代码:

string a = xnodePagedata.SelectSingleNode(//Control[@id='busRowOAppr2EIDLookUpUserControl']).Attributes["Value"].Value;

I have tried to use SelectSingleNode(string) but that is giving me a null reference exception. 我尝试使用SelectSingleNode(string),但这给了我一个null引用异常。 Kindly suggest how should I go about this one. 请建议我该如何进行。 I am an absolute beginer on XML. 我是XML的绝对入门者。

One possible way using the same approach : 一种使用相同方法的可能方法:

string a = 
    xnodePagedata.SelectSingleNode("//control[@id='busRowOAppr2EIDLookUpUserControl']/field/valuefield/@value")
                 .Value;

UPDATE : 更新:

In case there are multiple <valuefield> in one <control> and you want all value s, use SelectNodes() for example : 如果一个<control>有多个<valuefield>并且您想要所有value s,请使用SelectNodes()例如:

var values =
    xPagedata.SelectNodes("//control[@id='busRowOAppr2EIDLookUpUserControl']/field/valuefield/@value");
foreach (XmlNode value in values)
{
    Console.WriteLine(value.Value);
}

You can use XDocument : use Descendants("control") to get all controls then filter them using the Where clause then use SelectMany to get a flattened collection of values of valuefield. 您可以使用XDocument :使用Descendants("control")获取所有控件,然后使用Where子句过滤它们,然后使用SelectMany获取valuefield值的扁平集合。

XDocument doc = XDocument.Load(filepath);

var result = doc.Descendants("control")
                .Where(i => (string)i.Attribute("id") == "busRowOAppr2EIDLookUpUserControl")
                .SelectMany(i => i.Descendants("valuefield")
                                  .Select(j => j.Attribute("value")))
                .ToList();

And this is the result: 结果如下:

result  Count = 2    
[0] {value="709227"}    
[1] {value="_JohnDoe@Wonder.com"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM