读取XML节点并添加到datagrid

时间:2016-04-12 08:24:22

标签: c# xml

我有一个带有设置的XML文件。我想阅读所有name个节点的valueCollectionEntry,并将它们添加到DataGrid。

我认为一个好方法是使用List并使用XML文件的名称和值填充它,然后将其添加到DataGrid,如下所示:

List<Buffer> buffers = new List<Buffer>();
buffers.Add(new Buffer(){bufferName="username_testuser1",bufferValue="testuser2"});
bufferGrid.DataSource = buffers;

public class Buffer
{
  public string bufferName { get; set; }
  public string bufferValue { get; set; }
}

但我怎样才能阅读具体的CollectionEntry值?这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<Settings xmlns="Namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Category name="Commander">
    <Category name="ManualTestExecution">
      <Setting name="WindowLocation" legacyPath="Commander.GUI.ManualTestExecution.WindowLocation">336,263</Setting>
      <Setting name="WindowSize" legacyPath="Commander.GUI.ManualTestExecution.WindowSize">732,451</Setting>
    </Category>
  </Category>
  <Category name="Engine">
    <Setting name="Key Delay" legacyPath="BaseSettings.keyDelay">1</Setting>
    <Setting name="Wait Intermediate" legacyPath="BaseSettings.waitIntermediate">5000</Setting>
    <Setting name="Report Successful Execution of" legacyPath="XML.OutputLogLevel">2</Setting>
    <Collection name="Buffer" legacyPath="Buffer.Local">
      <CollectionEntry name="username_testuser1">testuser2</CollectionEntry>
      <CollectionEntry name="password_testuser1">!Welcome099</CollectionEntry>
      <CollectionEntry name="username_testuser2">testuser2</CollectionEntry>
    </Collection>
  </Category>
</Settings>

1 个答案:

答案 0 :(得分:0)

使用LINQ to XML,您可以使用

var monthname = 'AprilApril';
var month = monthname;
console.log(month.substr(0, (month.length/2))); // April
console.log(month) // AprilApril