I have a collection of custom objectswhich has storeid. I also have an XmlDocument which I query data from the database and constructed in memory. The StoreId property in the custome object corresponds to the "Value" object in the XML.I have to loop through my custom collection and match the StoreId to the "Value" attribute in the XML and set its "Checked" attribute equal to true. As shown in the XML below, all the "Checked" attribute values are set to false at the start.
<Tree>
<Node Text="Whole ">
<Node Text="America">
<Node Text="NewYork">
<Node Value="28" Checked="false " Text="NY1" />
<Node Value="29" Checked="false " Text="NY2" />
</Node>
<Node Text="Houston">
<Node Value="13 " Checked="false " Text="H1" />
<Node Value="14 " Checked="false " Text="H2" />
<Node Value="16 " Checked="false " Text="H3" />
<Node Value="19 " Checked="false " Text="H4" />
<Node Value="26 " Checked="false " Text="H5" />
</Node>
<Node Text="GeorgeTown">
<Node Value="21 " Checked="false " Text="G1" />
<Node Value="23 " Checked="false " Text="G2" />
<Node Value="25 " Checked="false " Text="G3" />
</Node>
</Node>
</Node>
</Tree>
My code is:
public class MyObject
{
public string StoreId { get; set; }
public bool HasValue { get; set; }
}
Code to update XML:
XmlDocument baseDocument = ConstructXMLFromDataBase();
XmlNodeList dataNodes = baseDocument.SelectNodes("//Tree/Node/Node/Node");
List<MyObject> myCollections = GetMyCollection();
foreach (var myCollection in myCollections)
{
foreach (XmlNode node in dataNodes)
{
//code to update
}
}
I believe it can be done easily with Linq to XML and I am very new to Linq to XML. Moreover most of the samples available on the internet are about updating XML loaded from a disc and not constructed in memory.
Thanks
//Tree/...searches for allTreetags. I think you meant/Tree/...which starts at the root.