简体   繁体   English

从xml文件中删除数字签名标签

[英]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文件如下所示

 <?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.. 但是rnode返回空值。

and i tried using Linq 我尝试使用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. 在这里signElement也返回了null值..所以两者都没有用。有人可以告诉我我哪里出了问题,还是可以告诉我删除数字签名的正确方法。

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" . 在这种情况下,为"Signature"

then call Remove() 然后调用Remove()

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

then save the xml back to a file if required; 然后根据需要将xml保存回文件;

doc.Save("output.xml");

See MSDN Remove() for more detail. 有关更多详细信息,请参见MSDN Remove()

Further, your LINQ to XML query 此外,您的LINQ to XML查询

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. 因此,您将获得第一个“ SignedInfo”节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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