0

I have some XML that looks similar to this:

<SolutionString>
  <Solutions>
    <Solution>
      <ID>1</ID>
      <Property>
        <Name>DriverSheave</Name>
        <Value>1VP34</Value>
      </Property>
      <Property>
        <Name>DriverBushing</Name>
        <Value>
        </Value>
      </Property>
      <Property>
        <Name>DrivenSheave</Name>
        <Value>AK49</Value>
      </Property>
      <Property>
        <Name>DrivenBushing</Name>
        <Value>
        </Value>
      </Property>
    </Solution>
    <Solution>
      <ID>2</ID>

For every ID number, the example above includes ID 1. I'd like to include all of its child elements into one line of a combobox.

To look similar to this

  1. DriverSheave = 1vp34, Driver Bushing = (nothing)/0, DrivenSheave = AK49,
  2. etc...

I have looked at Getting XML data into combobox, but it only shows how to get single items from XML into combo boxes.

Community
  • 1
  • 1
  • Where does `2` come from? Is that in a separate `` element? It would help if you could give a short but *complete* example of the XML, with two IDs. (And are you just looking for a `List` as the result?) – Jon Skeet Jun 25 '15 at 18:18
  • @JonSkeet the xml just contines with id 2, id 3, id4, ect in this exact same format, i didnt want to give you 100 lines of xml, and im looking for whichever way makes it easiest for this to be placed into a combobox. –  Jun 25 '15 at 18:18
  • You mean there isn't one "root" element per ID - you just keep reading Name/Property elements until you hit the next ID element? If so, can you change the format? (That's really ugly.) – Jon Skeet Jun 25 '15 at 18:20
  • @JonSkeet okay heres a full piece, that hits ID 2, and ID 2 looks identical to ID 1 but i didn't want to give you 1000 lines. –  Jun 25 '15 at 18:22
  • Right - so there *is* a root element, a `Solution` element. You can demonstrate that in a shorter space than you have, but at least we know what we're dealing with... – Jon Skeet Jun 25 '15 at 18:24

1 Answers1

2

So, for each entry, we have:

  • A Solution element containing:
    • An ID element
    • Some Property elements containing:
    • A Name element
    • A Value element (optional)

I would first transform the XML to that structure in memory, and then you can convert it to strings. A LINQ query should make that easy - you could either create classes, or just use anonymous types if this is all you need it for:

var doc = XDocument.Load(file);
var solutions = docs
    .Descendants("Solution")
    .Select(x => new {
        ID = (string) x.Element("ID"),
        Properties = x.Elements("Property").Select(p => new {
            Name = (string) p.Element("Name"),
            Value = (string) p.Element("Value")
        }).ToList()
    }).ToList();

Then you could use:

var items = solutions
   .Select(s => new {
       ID = s.ID,
       Text = string.Format("{0}. {1}", s.ID,
           string.Join(", ", s.Properties
                              .Select(p => string.Format("{0} = {1}",
                                                         p.Name,
                                                         p.Value ?? "(nothing/0)"))))
   }).ToArray();

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "ID";
comboBox.Items.AddRange(items);

(This is untested, and it may have some bracket mismatches, but should give you the basic idea.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • this looks like what i was looking for ill try this out thanks. –  Jun 25 '15 at 18:40
  • my xml is in XmlDocument form and not XDocument, will i need to convert this or will this still be fine for LINQ –  Jun 25 '15 at 18:52
  • @AndrewKucenski: It will be *much* easier if you start using LINQ to XML. It's simply a nicer XML API all round. – Jon Skeet Jun 25 '15 at 18:56
  • im getting a "does not contain a definition error at the ' .Select ' everything else seems to have no issue. –  Jun 25 '15 at 19:23
  • @AndrewKucenski: Do you have a `using` directive for `System.Linq`? Which `Select` is failing? – Jon Skeet Jun 25 '15 at 19:27
  • i have `System.linq` , and `System.Xml.linq` , and its failing at line `Properties = x.Elements("Property").Select(p => new {` –  Jun 25 '15 at 19:28
  • nvm mind i was missing an s on elements . #typing is hard :( –  Jun 25 '15 at 19:30
  • put it all in and ran it, and returned an output like this into my combobox. `1. = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null)` `2. = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null), = (null)` so it worked for the id values, but not the properties. –  Jun 25 '15 at 20:41
  • @Andrew: I see the problem - fixed now. – Jon Skeet Jun 25 '15 at 20:43
  • thanks this works, as a side/follow up, is there a simple way for me to pick and choose which of the properties items are displayed, as you saw in my last comment with the `= (null)` i have about 12 properties, however i really only need to display 6 of them, –  Jun 25 '15 at 20:51
  • @Andrew: That's a separate question. I suggest you try to solve it for yourself, and ask a new question if necessary. – Jon Skeet Jun 25 '15 at 20:52