简体   繁体   中英

xml parsing using linq

I have a combobox and when users select them xml is parsed.

if i choose item1, i need to select this "C:\\folder1" as a path. item 2: "C:\\folder2" item 3: "C:\\folder3"

my XML file:

<?xml version="1.0"?>
<main>
    <target>C:\folder1</target>
    <target>C:\folder2</target>
    <target>C:\folder3</target>
</main>

I am using Xdocument to do the parsing:

        private void pathselection()
    {
        XDocument pathdoc = XDocument.Load(@"C:\GUI\path.xml");
        var abc = from target in pathdoc.Descendants("target")
                  ...
                  ...

    }

now i am stuck over here. should i include if statements inside pathselection()? and how do i parse the path?

EDIT1:

Btw, i would like to store these path as strings

string selectedpath1 = "";

Given that the selectable items in the ComboBox match the items in the XML, you can get the target elements by index:

private void pathselection()
{
    XDocument pathdoc = XDocument.Load(@"C:\GUI\path.xml");
    var abc = pathdoc.Descendants("target")
                  .Where((e, i) => i == MyComboBox.SelectedIndex)
                  .Single();        
}

You might want to handle things like when the indexes don't match and you might want to use SingleOrDefault or FirstOrDefault to deal with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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