开发者

Reading XML between two child nodes

开发者 https://www.devze.com 2023-03-03 04:11 出处:网络
I\'m having a little trouble reading the values of all the sub child nodes between two child nodes. I have a function currently that is \"working\" but it\'s not adding the items to a listbox as it\'s

I'm having a little trouble reading the values of all the sub child nodes between two child nodes. I have a function currently that is "working" but it's not adding the items to a listbox as it's meant to do. I don't get any errors either.

The way the application works is that the name of all the xmls within a folder are added to a combobox once a file is selected in the combobox another combo box is then populated with all the Testcycle numbers that are in that XML. You then select a number within the cmobox box and then click a button and then i want all the values of the attributes filename and hashcode to then be written to a listbox. I.e. if there are 20 filename values and 20 hashcode values there should be 40 items in the 开发者_运维技巧listbox.

The only function not working is the adding to listbox one.

void HashMe::AddToListBox()
{
    String^ SelectedFile = comboBox1->SelectedItem->ToString();
    String^ SearchString = "*" + SelectedFile + "*.XML";
    int SelectedTC = int::Parse(comboBox2->SelectedItem->ToString());

    try
    {
        array<String^>^ FullPaths = IO::Directory::GetFiles("E:\\XML Folder\\", SearchString, System::IO::SearchOption::AllDirectories);
        int number = FullPaths->GetLength(0);

        for (int x = 0; x < number; x++)
        {
            String^ FullPath = FullPaths[x];

            XPathNavigator^ Root = XPathDocument(FullPath).CreateNavigator();
            for each (XPathNavigator^ Nav in Root->Select(L"//TestCycle[@Number = '" + SelectedTC + "']"))
            {
                listBox4->Items->Add(Nav->GetAttribute(L"FileName",String::Empty)).ToString();
                listBox4->Items->Add(Nav->GetAttribute(L"HashCode",String::Empty)).ToString();
            }
        }
    }
    catch (Exception^ e)
    {
        MessageBox::Show(e->ToString());
    }
}

The XML file structure is like this:

<?xml version="1.0" encoding="utf-8"?>
<Project Name="New">
  <TestCycle Number="1">
    <Files>
      <FileName File="C:\Users\brandonm\Documents\asd.xps" />
      <HashCode Code="AB-B5-85-EC-FE-C4-E2-41-09-6A-A8-77-69-A9-8D-1F" />
      <FileName File="C:\Users\brandonm\Documents\asdas.xps" />
      <HashCode Code="7F-30-12-76-C8-80-CA-03-EC-71-12-49-9E-56-9D-D8" />
      <FileName File="C:\Users\brandonm\Documents\asdasdasd" />
      <HashCode Code="09-37-69-EF-36-3B-FD-42-D2-37-2D-70-74-A6-ED-BA" />
    </Files>
  </TestCycle>
  <Project Name="New">
    <TestCycle Number="2">
      <Files>
        <FileName FileName="C:\Users\brandonm\Documents\asd.xps" />
        <HashCode HashCode="AB-B5-85-EC-FE-C4-E2-41-09-6A-A8-77-69-A9-8D-1F" />
        <FileName FileName="C:\Users\brandonm\Documents\asdas.xps" />
        <HashCode HashCode="7F-30-12-76-C8-80-CA-03-EC-71-12-49-9E-56-9D-D8" />
        <FileName FileName="C:\Users\brandonm\Documents\asdasd.xps" />
        <HashCode HashCode="E4-EF-33-AE-24-23-00-37-FD-E2-20-60-5E-68-C0-54" />
      </Files>
    </TestCycle>
  </Project>

Any help would be greatly appreciated. I think there may be an issue with my XPath queries. Thanks


First you need to correct your XML, as it's inconsistent and malformed:

  • You have more open Project elements than close elements
  • You don't have a root element to contain each Project element
  • Under TestCycle #1 your FileName elements' attribute name is File, whereas under TestCycle #2 your FileName elements' attribute name is FileName; reconcile these
  • Under TestCycle #1 your HashCode elements' attribute name is Code, whereas under TestCycle #2 your HashCode elements' attribute name is HashCode; reconcile these

I'll assume you fix this to something like the following:

<?xml version="1.0" encoding="utf-8"?>
<Projects>
  <Project Name="New">
    <TestCycle Number="1">
      <Files>
        <FileName File="C:\Users\brandonm\Documents\asd.xps" />
        <HashCode Code="AB-B5-85-EC-FE-C4-E2-41-09-6A-A8-77-69-A9-8D-1F" />
        <FileName File="C:\Users\brandonm\Documents\asdas.xps" />
        <HashCode Code="7F-30-12-76-C8-80-CA-03-EC-71-12-49-9E-56-9D-D8" />
        <FileName File="C:\Users\brandonm\Documents\asdasdasd" />
        <HashCode Code="09-37-69-EF-36-3B-FD-42-D2-37-2D-70-74-A6-ED-BA" />
      </Files>
    </TestCycle>
  </Project>
  <Project Name="New">
    <TestCycle Number="2">
      <Files>
        <FileName File="C:\Users\brandonm\Documents\asd.xps" />
        <HashCode Code="AB-B5-85-EC-FE-C4-E2-41-09-6A-A8-77-69-A9-8D-1F" />
        <FileName File="C:\Users\brandonm\Documents\asdas.xps" />
        <HashCode Code="7F-30-12-76-C8-80-CA-03-EC-71-12-49-9E-56-9D-D8" />
        <FileName File="C:\Users\brandonm\Documents\asdasd.xps" />
        <HashCode Code="E4-EF-33-AE-24-23-00-37-FD-E2-20-60-5E-68-C0-54" />
      </Files>
    </TestCycle>
  </Project>
</Projects>

Your code is written as though FileName and HashCode are attributes of TestCycle, but in reality they're elements under TestCycle/Files with respective attributes of File and Code. Clearly your code is in error here; replace your inner for each loop with something like:

String^ fmt = L"//TestCycle[@Number = '{0}']/Files/FileName/@File | //TestCycle[@Number = '{0}']/Files/HashCode/@Code";
for each (XPathNavigator^ Nav in Root->Select(String::Format(fmt, SelectedTC)))
    listBox4->Items->Add(Nav->Value);

or

for each (XPathNavigator^ Cycle in Root->Select(String::Format(L"//TestCycle[@Number = '{0}']", SelectedTC)))
    for each (XPathNavigator^ Nav in Cycle->Select(L"Files/FileName/@File | Files/HashCode/@Code"))
        listBox4->Items->Add(Nav->Value);
0

精彩评论

暂无评论...
验证码 换一张
取 消