Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a requirement wherein I have to update the values of each child nodes with the same name to null (""). The data is of type XElement (XML). Below is the snippet i tried so far.
C#
if (xmlData.Elements("attachment").Any()) {
                    foreach (XElement nodes in xmlData.Elements("attachment"))
                    {
                        if (nodes.Value != "") {
                            xmlData.SetElementValue(nodes.Name, "");
                        }
                    }
                }
                if (xmlData.Elements("comment").Any() && xmlData.Elements("comment").Elements("comment_attachment").Any()) {
                    foreach (XElement nodes in xmlData.Elements("comment").Elements("comment_attachment")) {
                        if (nodes.Value != "") {
                            xmlData.SetElementValue(nodes.Name, "");
                        }
                    }
                }


Here the xmlData is the XElement object containing a huge XML parent node. Inside this node I have to clear the contents of the nodes attachment and comment_attachment, which are essentially files encoded as binary data and stored in XML.

I tried to use the SetElementValue method which sets only the first child value to null, but not for all the children (attachment or comment_attachment nodes).

Are there any other alternatives for this?

Thanks in advance.
Posted
Comments
Maciej Los 17-Aug-15 15:55pm    
Your requirements are bit strange. If you change node's name to null, you'll get something like this: <></>. This might cause several problems in serialization process.
Sriram Mani 18-Aug-15 2:35am    
Hi Maciej. Sorry if the description made a confusion. I wanted the node's value to be null. Not the node's name to be null. Suppose for Ex: <attachment name="blahblah.doc">xxxxxxxxxyyyyy....xxxyyy</attachment>. I want it to be <attachment name="blahblah.doc"></attachment>

Maciej Los 18-Aug-15 3:57am    
Please, see my answer.

Try the RemoveNodes method[^]:
C#
foreach (XElement node in xmlData.Elements("attachment"))
{
    node.RemoveNodes();
}
foreach (XElement node in xmlData.Elements("comment").Elements("comment_attachment"))
{
    node.RemoveNodes();
}

If you want to clear the attributes as well, use the RemoveAll method[^].

NB: This code will only find <attachment> and <comment> elements which are direct children of the xmlData node.
 
Share this answer
 
Comments
Maciej Los 17-Aug-15 15:56pm    
Sounds reasonable, a 5!
Sriram Mani 18-Aug-15 2:44am    
Thanks for the Suggestion Richard. I don't need to remove the nodes. Rather I need to remove the contents (or values) inside the nodes. I already tried with Remove nodes as the first step. But the requirement is to retain the nodes in the same hierarchy and to remove the contents alone. In my case, the contents are binary data which doesn't make any sense to read or to be displayed. But retaining the node is essential, since the node contains information about attachment like the attachment name and size as attributes.

The reason why I used SetElementValue is that I could explicitly set the attachment value to be null clearing the binary data inside attachment nodes and still retains the hierarchy and attributes. But the problem is, it clears the contents of first child alone. I have more than one attachment nodes and comment_attachment nodes inside a parent node, which also should be cleared.
Sriram Mani 18-Aug-15 3:21am    
Oops. Sorry Richard. My bad. I tried as xmlData.RemoveNodes() rather than nodes.RemoveNodes(). This actually works. Thanks for the solution!
As to the OP's comments...

Have a look at example:
C#
//create sample xml (input)
string xcontent = @"<Boards>
    <Board class='support' appid='1'>
        <Notes>
            <Note class='message' id='1' source='client'>Good morning! My application hangs when i try to copy large portion of text to the clipboard.</Note>
            <Note class='message' id='2' source='stuff'>Good morning!</Note>
            <Note class='question' id='3' source='stuff'>Have you tried to copy less portion of text?</Note>
            <Note class='message' id='4' source='client'>Wait a moment. I'll try.</Note>
            <Note class='message' id='5' source='client'>Sorry, still the same error.</Note>
            <Note class='message' id='6' source='stuff'>Thank you! Seems, it's a bug. I'll report it to the developer.</Note>
            <Note class='question' id='7' source='stuff'>Do you have another question?</Note>
            <Note class='message' id='8' source='client'>No. Thank you.</Note>
            <Note class='message' id='9' source='stuff'>Thank you. Soon you'll get information about an update.</Note>
        </Notes>
    </Board>
</Boards>";
XDocument xdoc = XDocument.Parse(xcontent);

//get nodes with specific attribute;
var nodesToClean = xdoc.Descendants("Note").Where(x=>x.Attribute("class").Value=="question");

//replace value with empty string
foreach (XElement xele in nodesToClean)
{
    xele.Value = string.Empty;
}

//document has been changed!!!


Result:
XML
<Boards>
  <Board class="support" appid="1">
    <Notes>
      <Note class="message" id="1" source="client">Good morning! My application hangs when i try to copy large portion of text to the clipboard.</Note>
      <Note class="message" id="2" source="stuff">Good morning!</Note>
      <Note class="question" id="3" source="stuff"></Note>
      <Note class="message" id="4" source="client">Wait a moment. I'll try.</Note>
      <Note class="message" id="5" source="client">Sorry, still the same error.</Note>
      <Note class="message" id="6" source="stuff">Thank you! Seems, it's a bug. I'll report it to the developer.</Note>
      <Note class="question" id="7" source="stuff"></Note>
      <Note class="message" id="8" source="client">No. Thank you.</Note>
      <Note class="message" id="9" source="stuff">Thank you. Soon you'll get information about an update.</Note>
    </Notes>
  </Board>
</Boards>
 
Share this answer
 
Comments
Sriram Mani 19-Aug-15 2:23am    
Thanks for the solution Maciej. The solution which Richard provided earlier in this post actually worked fine in my scenario.
Maciej Los 19-Aug-15 2:27am    
You're very welcome. Can you accept my answer as a solution? Note: you can accept more then one solution if it meets your needs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900