What is wrong with this code that I am not able to get the companyname element?
The root element of your xml is CompanyName
. So what your code is doing, it's essentially asking 'give me all CompanyName
elements that are children of my root CompanyName
element'. Hence the list is empty.
how can I get list of all SubContainer
elements.
You can use
var subContainers = doc.Root.Descendants("SubContainer");
when I got the list of SubContainer
elements, how can I read read and change its content?
foreach (var subContainer in subContainers)
{
foreach (var attrContainer in subContainer.Elements("AttrContainer"))
{
var attr = attrContainer.Elements("Attr").FirstOrDefault();
if (attr != null)
{
var oldValue = attr.Attribute("type").Value;
attr.Attribute("type").Value = "something completely different";
}
}
}
This reads and changes the type on each first Attr
element (assuming one exists) in all AttrContainers
in all SubContainers
- hopefully you can derive something meaningful out of that.