I have modified the structure of d xml file. i want to edit value of visible
Asked
Active
Viewed 266 times
0
-
1You need to expand this question with sample XML (before and after the change) to get a decent answer – Steve Townsend Nov 17 '10 at 19:44
-
2The sample XML provide is not valid. Tags c and d have no closing brace. – Scott Markwell Nov 17 '10 at 19:49
-
@Scott - nor does the second 'b' – KevinDTimm Nov 17 '10 at 19:52
-
its an example of the structure the issue is that i am unable to edit the attribute value from aaa to bb – user428747 Nov 17 '10 at 19:56
-
1@user428747: It's not an example of a valid XML file, which makes it hard to tell what the *correct* file would look like. – Jon Skeet Nov 17 '10 at 19:57
2 Answers
6
Well, LINQ to XML makes it very easy to manipulate XML documents, assuming they're small enough to be sensibly loaded into memory.
For example:
var doc = XDocument.Load("Foo.xml");
foreach (var element in doc.Descendants("c"))
{
element.SetAttributeValue("value", "bb");
}
doc.Save("Bar.xml");
Now that will set the value
attribute for every c
element. It's not clear whether or not that's what you want. If it's not, please edit your question to make it more specific.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
Actually for it to work("SetAttribute" does not exist), use that code: element.SetAttributeValue("value", "bb"); – SwissCoder Nov 23 '10 at 15:03
-
1
1
You can use such code pattern:
bool foobar()
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(FileName);
XmlNodeList ns = doc.SelectNodes("a/d/e/f");
if (ns.Count == 1)
{
ns[0].Attributes["visible"].Value = true;
doc.Save(FileName);
return (true);
}
else
return (false);
}
catch (Exception e)
{
return (false);
}
}

texnedo
- 166
- 1
- 6
-
Using true/false instead of exceptions is a highly non-idiomatic (and lossy) way of indicating success or failure in .NET. Exceptions are there for a reason - use them! – Jon Skeet Nov 18 '10 at 10:23
-
Do you mean that i should populate exception to parent code? And what about performance in this situation? – texnedo Nov 19 '10 at 08:05