I can read a xml file to the console. But I need only some elements from the xml file. Like Sender Name, Postalcode, weight and value. Because now all the element from the xml file are returned.
Asked
Active
Viewed 128 times
1 Answers
2
You can try XDcoument
with LINQ
XDocument xdoc = XDocument.Load($"XMLFile1.xml");
var items = xdoc.Descendants("Parcel")
.Select(xelem => new
{
Name = xelem.Element("Sender").Element("Name").Value,
PostalCode = xelem.Element("Sender").Element("Address").Element("PostalCode").Value,
Weight = xelem.Element("Weight").Value,
Value = xelem.Element("Value").Value
});
foreach (var item in items)
{
Console.WriteLine($"{ item.Name} - { item.PostalCode} - { item.Weight} - { item.Value}");
}

Krishna Varma
- 4,238
- 2
- 10
- 25
-
Thank you. But then I don't need all the other code? Because if I run your script I only get the name: Klaas ykken groot B.V. seti Aad – mightycode Newton Aug 30 '21 at 08:12
-
@mightycodeNewton you dont need any other code. I have updated the post to get what is requried. – Krishna Varma Aug 30 '21 at 08:13
-
@mightycodeNewton, have updated with much simplified version. please have a look – Krishna Varma Aug 30 '21 at 08:18