thejus_r
thejus_r

Reputation: 321

Removing digital Signature tags from an xml file

i need to know how the digital signature tags can be removed. its a enveloped digital signature

my xml file looks like the following

 <?xml version="1.0" encoding="utf-8" ?> 
 <questionset>
    <question category="graph" /> 
 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
    </SignedInfo>
 </Signature>
 </questionset>

i tried somethings

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.PreserveWhitespace = false;
 xmlDoc.Load(new XmlTextReader(Doc));
 XmlNode rnode = xmlDoc.SelectSingleNode("questionset/Signature");
 XmlNode parent = rnode.ParentNode;
 parent.RemoveChild(rnode);
 string newXML = xmlDoc.OuterXml;
 XmlTextWriter xmlWriter = new XmlTextWriter(filename2, new UTF8Encoding(false));
 xmlDoc.WriteTo(xmlWriter);
 xmlWriter.Close();

but rnode returned null value..

and i tried using Linq

 XElement document = XElement.Load(Doc);
 XElement signElement = document.Descendants("Signature").FirstOrDefault<XElement>();
 signElement.Remove();

here also signElement returned null value.. so both didnt work.can someone tell me where i went wrong or can you tell me the right approach to remove the digital signature.

Upvotes: 0

Views: 2432

Answers (1)

Sam Leach
Sam Leach

Reputation: 12966

XElement document = XElement.Load(Doc);

IEnumerable<XElement> signElements = document.Descendants("questionSet").Elements("Signature");

will get you the elements you wish to remove. In this case, "Signature".

then call Remove()

document.Descendants("questionSet").Elements("Signature").Remove();

then save the xml back to a file if required;

doc.Save("output.xml");

See MSDN Remove() for more detail.

Further, your LINQ to XML query

XElement signElement = document.Descendants("Signature").FirstOrDefault<XElement>();

would have gotten the descendants/children of "Signature" and then returned the first item in the collection. So you would have gotten the first "SignedInfo" node.

Upvotes: 1

Related Questions