2

I'm having trouble with a line of code.

I have an XML that has the following data (ID 0, 1, 2, 3, 4 are all the same in this XML. This can be different in another generated XML):

<dissasemblypart>
  <ID>0</ID>
  <PartType>Tube</PartType>
</dissasemblypart>
<dissasemblypart>
  <ID>5</ID>
  <PartType>FlatSheetMetal</PartType>
</dissasemblypart>

Now I want to select the Part Type by the ID in the XML but I don't know how to achieve it. I already have the xml reading code, so I will skip that part. All I want to achieve is that the code connects ID and PartType together.

var PartType = partsFromFile.Descendants("PartType");
var PartID = partsFromFile.Descendants("ID");

  foreach (var id in PartID)
  {
     foreach (var type in PartType)
     {
         WriteLine($"{id.Value}{type.Value}");
     }
  }

This is the code I currently have, this returns every PartType for every ID, which is close but I know its not what I want returned.

A push in the right way would be highly appreciated.

ronald1993
  • 75
  • 1
  • 10

3 Answers3

3

You could use Linq to Xml.

var result = partsFromFile.Descendants("dissasemblypart")
                         .Where(x=>Convert.ToInt32((string)x.Element("ID"))==idToSearch)
                         .Select(x=>(string)x.Element("PartType"))
                         .First(); 
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • Sorry if this is a stupid question, but do I use this code instead of my code? – ronald1993 Jan 21 '20 at 10:09
  • @ronald1993 yes , you could use the code shown in the answer, instead of the explicit loop based approach as shown in OP. – Anu Viswan Jan 21 '20 at 10:10
  • I'm getting the an error on the following part, assuming the "idToSearch" should be changed into any of the ID's I want to know.: (string)x.Element("ID")==0), the error says: Operator '==' cannot be applied to operands of type 'string' and 'int' – ronald1993 Jan 21 '20 at 10:16
  • @ronald1993 since idToSearch is an integer, you need to convert the element value to int as well. Please check the updated answer – Anu Viswan Jan 21 '20 at 10:19
  • I feel like we're almost there, the code does not give errors anymore. Thankyou for that. The only thing I'm encountering now is that its returning the following: System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] instead of "Tube" what I expected to be returned. – ronald1993 Jan 21 '20 at 10:28
  • @ronald1993 Sorry, that was my mistake. I assumed you could have duplicate ids. Please check the updated code. – Anu Viswan Jan 21 '20 at 10:34
  • No the ID is always unique, the PartType can have duplicates. System.InvalidOperationException: 'Sequence contains no elements' This must mean that its not getting any results I'm guessing? – ronald1993 Jan 21 '20 at 10:43
  • Nevermind, I got it! I think i made a typo in "Dissasembly", it was supposed to be "Disassembly". My bad, thankyou for your help! – ronald1993 Jan 21 '20 at 10:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206349/discussion-between-ronald1993-and-anu-viswan). – ronald1993 Jan 21 '20 at 11:11
0

You could use XML deserialization in a C# object. In this case I would deserialize it in a list of "custom" objects in order to use the full potential of the lists or wanting to LINQ based on what you have to do. I am attaching below a link for the use of XML deserialization: How to deserialize xml to object

0

If you look how you consume XML Data then the correct way is to create a DTO class. The DTO class reads the XML Data with a XML Deserializer. Then you can copy that data to another class.