XmlIdSignedXml.cs
using System;
using System.Security.Cryptography.Xml;
using System.Xml;
public sealed class XmlIdSignedXml : SignedXml
{
public static readonly string XmlIdUrl = "http://www.w3.org/XML/1998/namespace";
private bool m_strict;
public XmlIdSignedXml() : base()
{
Strict = false;
return;
}
public XmlIdSignedXml(XmlDocument document) : base(document)
{
Strict = false;
return;
}
public XmlIdSignedXml(XmlElement element) : base(element)
{
Strict = false;
return;
}
public bool Strict
{
get { return m_strict; }
set { m_strict = value; }
}
public override XmlElement GetIdElement(XmlDocument document, string idValue)
{
if(idValue == null)
throw new ArgumentNullException("idValue", "Need an ID value to search for");
if(document == null)
return null;
XmlNamespaceManager nsManager = new XmlNamespaceManager(document.NameTable);
nsManager.AddNamespace("xml", XmlIdUrl);
string searchString = null;
if(idValue.IndexOf('\'') == -1)
searchString = "'" + idValue + "'";
else if(idValue.IndexOf('\"') == -1)
searchString = "\"" + idValue + "\"";
else
throw new ArgumentException("idValue", "Cannot search for an xml:id containing both single and double quotes.");
XmlNodeList xmlIdNodes = document.SelectNodes("//*[@xml:id=" + searchString + "]", nsManager);
if(xmlIdNodes.Count > 1)
throw new InvalidOperationException("Search for a non-unique xml:id");
else if(xmlIdNodes.Count == 1)
return xmlIdNodes[0] as XmlElement;
if(Strict)
return null;
else
return base.GetIdElement(document, idValue);
}
}