开发者

combobox selects an index, then this index contains a string from xml text content

开发者 https://www.devze.com 2023-02-23 05:11 出处:网络
hi i am struggling to get text content out from my xml file as a string. First, I select a combo box which ha开发者_运维问答s these items:

hi i am struggling to get text content out from my xml file as a string.

First, I select a combo box which ha开发者_运维问答s these items:

  1. 6.00
  2. 6.10
  3. 6.20

Secondly, if for example i select "6.00" i will select the string "C:\folder1" 6.10 selects "C:\folder2" 6.20 selects "C:\folder3"

my XML file(path.xml) is as follow:

<main>
<one>C:\folder1</one>
<two>C:\folder2</two>
<three>C:\folder3></three>
</main>

so basically what i need is, to parse this xml file and get the text content. how do i go about doing this? i tried linq parsing:

    var prods = from s in
                    (from c in XElement.Load(@"C:\GUI\path.xml").Descendants("main")
                     where c.Value.Equals("?????")
                     select c).Descendants()
                select new main()
                {
                    path = s.Value,
                };

i am kind of stuck over here because it seems that whenever i use linq parsing, the strings are not differentiated according to the needs of my combo box


Try this linq query:

string elementName = ... ;  // see below

var prods = from c in XElement.Load("XMLFile1.xml").Descendants()
            where c.Name.LocalName.Equals(elementName)
            select c.Value;

Then you just need to convert the numeric value of the ComboBox SelectedIndex to a canonical value ("one", "two", or "three") to match the elements in the XML file. If you only have those three values then a simple switch() statement would do the trick:

string elementName = "one";
switch (comboBox.SelectedIndex)
{
  case 2:
    elementName = "two";
    break;

  case 3:
    elementName = "three";
    break;
}
0

精彩评论

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