none
XML, XDocument, LinQ

    Question

  • Hello,
     
    My target is a new list
      Name = "nominalValue", state=OK, value = 20
      Name = "numberOfUnits", state=OK, value = 20
      Name = "Index", state=OK, value = BE1

    var qryTest3 = (
    from a in elList.Attributes().AsEnumerable()
       where a.Name.LocalName == "name" && a.Name.LocalName == "state" && a.Name.LocalName == "value"
    select new
    {
       Name = a.Name.LocalName,
       State = a.Value
       Value = 
    }).ToList();

               


    My problem is not working, however. Maybe somebody can help me.


    My XML
    <properties>
     <orderProperties>
      <orderProperty name = "nominalValue" state = "ok" value = "20" />
      <orderProperty name = "numberOfUnits" state = "ok" value = "20" />
      <orderProperty name = "Index" state = "ok" value = "BE1" />
     </orderProperties>
    My attempts
    Works a little bit, but now as I need it.
    var elList = xDoc.Element("control")?.Element("controlResponse")?.Element("properties")?.Element("orderProperties").Elements("orderProperty");
    //XmlNodeList orderNodes = doc.SelectNodes("//orderProperty");
    List<Dictionary<string, string>> orderListProperties = new List<Dictionary<string, string>>();
    var qryTest = (
    from a in elList.Attributes().AsEnumerable()
     //where a.Name.LocalName == "state"
    select new
    {
     Key = a.Name.LocalName,
     Value = a.Value
    }).ToList();
    var qryTest2 = elList.ToDictionary(p => p.Attribute("name").Value, p => p.Attribute("state").Value);
    test
    Thursday, February 8, 2018 6:03 PM

Answers

  • //Doesn't matter how the XML was loaded, just getting and XDocument here...
    var xml = "<properties>" +
                "   <orderProperties>" +
                "      <orderProperty name=\"nominalValue\" state=\"ok\" value=\"20\" />" +
                "      <orderProperty name=\"numberOfUnits\" state=\"ok\" value=\"20\" />" +
                "      <orderProperty name=\"Index\" state=\"ok\" value=\"BE1\" />" +
                "   </orderProperties>" +
                "</properties>";
    var doc = XDocument.Parse(xml);
    
    //If you want it in a list then use ToList
    var orderProperties = from e in doc.XPathSelectElements("//orderProperty")
                            select new {
                                Name = e.Attribute("name")?.Value,
                                State = e.Attribute("state")?.Value,
                                Value = e.Attribute("value")?.Value
                            };


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 8, 2018 6:39 PM
    Moderator

All replies

  • //Doesn't matter how the XML was loaded, just getting and XDocument here...
    var xml = "<properties>" +
                "   <orderProperties>" +
                "      <orderProperty name=\"nominalValue\" state=\"ok\" value=\"20\" />" +
                "      <orderProperty name=\"numberOfUnits\" state=\"ok\" value=\"20\" />" +
                "      <orderProperty name=\"Index\" state=\"ok\" value=\"BE1\" />" +
                "   </orderProperties>" +
                "</properties>";
    var doc = XDocument.Parse(xml);
    
    //If you want it in a list then use ToList
    var orderProperties = from e in doc.XPathSelectElements("//orderProperty")
                            select new {
                                Name = e.Attribute("name")?.Value,
                                State = e.Attribute("state")?.Value,
                                Value = e.Attribute("value")?.Value
                            };


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 8, 2018 6:39 PM
    Moderator
  • Hello CoolDadTx,
    That is it.
    Thanks!
    With best regards Markus
    Friday, February 9, 2018 5:35 PM