本文整理汇总了C#中StringBuilder.EncodeForXmlDocument方法的典型用法代码示例。如果您正苦于以下问题:C# StringBuilder.EncodeForXmlDocument方法的具体用法?C# StringBuilder.EncodeForXmlDocument怎么用?C# StringBuilder.EncodeForXmlDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringBuilder
的用法示例。
在下文中一共展示了StringBuilder.EncodeForXmlDocument方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExtensionMethods_EncodeForXmlDocument_WhenInvalidXmlDocument_ExpectException
public void ExtensionMethods_EncodeForXmlDocument_WhenInvalidXmlDocument_ExpectException()
{
//------------Setup for test--------------------------
var sb = new StringBuilder("aa");
//------------Execute Test---------------------------
sb.EncodeForXmlDocument();
}
示例2: ExtensionMethods_EncodeForXmlDocument_WhenValidUnicodeXmlDocument_ExpectStream
public void ExtensionMethods_EncodeForXmlDocument_WhenValidUnicodeXmlDocument_ExpectStream()
{
//------------Setup for test--------------------------
byte[] bytes = { (byte)'<', (byte)'x', (byte)'/', (byte)'>' };
var msg = Encoding.Unicode.GetString(bytes);
var sb = new StringBuilder(msg);
//------------Execute Test---------------------------
using(var result = sb.EncodeForXmlDocument())
{
//------------Assert Results-------------------------
Assert.IsNotNull(result);
Assert.AreEqual(0, result.Position);
}
}
示例3: ExtensionMethods_EncodeForXmlDocument_WhenValidUTF8XmlDocument_ExpectStream
public void ExtensionMethods_EncodeForXmlDocument_WhenValidUTF8XmlDocument_ExpectStream()
{
//------------Setup for test--------------------------
const string msg = "<x>test message</x>";
var sb = new StringBuilder(msg);
//------------Execute Test---------------------------
using(var result = sb.EncodeForXmlDocument())
{
//------------Assert Results-------------------------
Assert.IsNotNull(result);
Assert.AreEqual(0, result.Position);
}
}
示例4: SignXml
public StringBuilder SignXml(StringBuilder xml)
{
if (xml == null || xml.Length == 0)
{
throw new ArgumentNullException("xml");
}
// remove the signature element here as it does not pick up correctly futher down ;(
xml = RemoveSignature(xml);
using(var s = xml.EncodeForXmlDocument())
{
var doc = new XmlDocument();
doc.Load(s);
SetServerID(doc);
var result = new StringBuilder();
using (var sw = new StringWriter(result))
{
doc.Save(sw);
}
// remove the crapy encoding header
result = result.CleanEncodingHeaderForXmlSave();
return result;
}
}
示例5: VerifyXml
public bool VerifyXml(StringBuilder xml)
{
if (xml == null || xml.Length == 0)
{
throw new ArgumentNullException("xml");
}
using(Stream s = xml.EncodeForXmlDocument())
{
var doc = new XmlDocument();
doc.Load(s);
// Validate server ID, this is a check which can be done quickly in order to skip loading the whole file for verification
var serverID = GetServerID(doc);
/*
* NOTE :
*
* This magical check is here for shipping resources
* It enables the server on first start to resign the resource such that
* the end user's install can view and execute them ;)
*
* To ship a resource you need to do the following :
*
* 1) Set the type to Unknown
* 2) Give the resource a server ID of our InternalServerID
* 3) Remove any existing signing data
*
*/
if (serverID != ServerID && serverID != InternalServerID)
{
return false;
}
// Find the "Signature" node and add it to the SignedXml object
var signedXml = new SignedXml(doc);
var nodeList = doc.GetElementsByTagName("Signature");
// allow unsigned resources with our internal server ID
if (nodeList.Count == 0 && serverID == InternalServerID)
{
return true;
}
signedXml.LoadXml((XmlElement) nodeList[0]);
var result = (serverID == ServerID && signedXml.CheckSignature(_serverKey)) ||
(serverID != InternalServerID == signedXml.CheckSignature(_systemKey));
// Check if signed by the server or the system
return result;
}
}
示例6: SignXml
public StringBuilder SignXml(StringBuilder xml)
{
if (xml == null || xml.Length == 0)
{
throw new ArgumentNullException("xml");
}
// remove the signature element here as it does not pick up correctly futher down ;(
xml = RemoveSignature(xml);
using(Stream s = xml.EncodeForXmlDocument())
{
var doc = new XmlDocument();
doc.Load(s);
SetServerID(doc);
// Create a reference to be signed and add
// an enveloped transformation to the reference.
var reference = new Reference
{
Uri = ""
};
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
var signedXml = new SignedXml(doc)
{
SigningKey = _serverKey
};
signedXml.AddReference(reference);
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
var xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
if (doc.DocumentElement != null)
{
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
}
StringBuilder result = new StringBuilder();
using (StringWriter sw = new StringWriter(result))
{
doc.Save(sw);
}
// remove the crapy encoding header
result = result.CleanEncodingHeaderForXmlSave();
return result;
}
}