简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM