C#如何获取XML文件的所有用户

时间:2018-04-18 17:03:37

标签: c# xml

我创建了一个xml文件,并存储了我的用户。问题是,我只得到第一个。如何设置显示XML文件的所有用户?

var mydata = [];
var intervalID = setInterval( function addsample() {
      var newval = Math.random() * 10;
    var mysample = { 
        timestamp: new Date(), 
        value: newval };
    mydata.push( mysample );
    console.log("sample count: " + mydata.length);
    if (mydata.length == 100) clearInterval(intervalID);

    MG.data_graphic({
    title: "Time Series Plot",
    data: mydata,
        interpolate: d3.curveStep,
        chart_type: 'line',
    height: 400,
    width: 600,
    xax_count: 3,
    x_accessor: 'timestamp',
    y_accessor: 'value',
    area: false,
    target: '#chart',
    transition_on_update: false //here's the argument
    });

  },1000);

1 个答案:

答案 0 :(得分:2)

我不确定什么不适合你,但这对我有用。我使用//Login作为路径选择器。确保您的XML文件实际上有多个用户。

void Main()
{
    //Create();
    View();

    // Output:
    // User1
    // User2
}

private void Create()
{
    string path = "C:\\Code\\Sandbox\\Accounts.xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    XmlElement root = doc.CreateElement("Login");
    XmlElement user = doc.CreateElement("user");
    user.InnerText = "User2";
    root.AppendChild(user);
    doc.DocumentElement.AppendChild(root);
    doc.Save(path);
    Console.WriteLine("Created SuccesFully!");
}

private void View()
{
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load("C:\\Code\\Sandbox\\Accounts.xml");
    foreach (XmlNode person in xdoc.SelectNodes("//Login"))
    {
        Console.WriteLine(person["user"].InnerText);
    }
}

这是生成的XML。我插入了一个根Logins元素。

<Logins>
  <Login>
    <user>User1</user>
  </Login>
  <Login>
    <user>User2</user>
  </Login>
</Logins>