Search code examples
c#xmlxml-parsingmultiple-columnsopenfiledialog

Reading Multiple XML Files and Order Listview


Hi good afternoon my question is:

Im writing a code to:

  1. Read Multiple files: Ican read only one file with the openfiledialog if i select more than one the program only takes one, i think i can use a foreach but i dont know how to implement.

  2. Order the xml nodes into a list view, i have a list view and the output has to be like this:


Test Name     Limit Judgment    Measurement    High Limit     Low Limit
Main__checkt      0                 3               5            3
Main__Initia      0                 4               4            3 
Main__Serial      0                 3               4            3

But my output is this:


Test Name     Limit Judgment    Measurement    High Limit     Low Limit
Main__Initia      0                 3               5            3
Main__Serial      0                 3               5            3 
Main__Cylind      0                 3               5            3

The programa takes the first row and iterate in the different columns how i can order this?

This is a sample of my XML File:

XML FILE

This is the code:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "XML | *.xml";
    ofd.Multiselect = true;
    ofd.ShowDialog();
    XmlDocument xml = new XmlDocument();
    xml.Load(ofd.FileName); // suppose that myXmlString contains "<Names>...</Names>"
    ListViewItem lvi;

    XmlNodeList xnList = xml.SelectNodes("/LogBatch/LogTestplan/LogTest");
    foreach (XmlNode xn in xnList)
    {

        string tnam = xn["TestName"].InnerText;

        lvi = new ListViewItem();
        lvi.Text = tnam;
        listView1.Items.Add(lvi);

        XmlNodeList xnList2 = xml.SelectNodes("/LogBatch/LogTestplan/LogTest/LogLimit");
        foreach (XmlNode xn2 in xnList2)
        {

            string limjud = xn2["LimitJudgment"].InnerText;
            string limlm = xn2["LimitLastMeasuredValue"].InnerText;
            string limhl = xn2["LimitHighLimit"].InnerText;
            string limlw = xn2["LimitLowLimit"].InnerText;

            lvi.SubItems.Add(limjud);
            lvi.SubItems.Add(limlm);
            lvi.SubItems.Add(limhl);
            lvi.SubItems.Add(limlw);
            string nomval = "4";
            lvi.SubItems.Add(nomval);
            string devstd = "0.46291";
            lvi.SubItems.Add(devstd);
            string cp = "0.72";
            lvi.SubItems.Add(cp);
        }
    }
}

Help please. Thank you. Best Regards.


Solution

  • Don't expect for complete code since your question is too broad for that1. But here are the main points to accomplish both requirements :

    1. When using Multiselect mode, you should check FileDialog.FileNames instead of singular FileName property. At this point you must be able to easily iterate through FileNames and load each of them into XmlDocument object.

    2. To get <LogLimit> node that correspond to current <LogTest> in your outer foreach loop you should use XPath relative to current <LogLimit>.

    So instead of using full path :

    XmlNodeList xnList2 = xml.SelectNodes("/LogBatch/LogTestplan/LogTest/LogLimit");
    

    try relative path :

    XmlNodeList xnList2 = xn.SelectNodes("./LogLimit");
    

    or simply :

    XmlNodeList xnList2 = xn.SelectNodes("LogLimit");
    

    You may want to consider SelectSingleNode() to replace SelectNodes() in context where you know exactly that there won't be more than one node satisfies the XPath.

    1) Post separate questions for each particular problem. And instead of throwing all your codes and full XML file content, try to create a Minimal, Complete, and Verifiable example. The example will help a lot in clarifying the problem, especially when We have limitation in writing English (I almost never write/speak English other than in this site too :p). You may learn something or even find a solution yourself in the process of creating the example