技术开发 频道

浅析Web Service适配器

后续说明

    为了简化后续个模式的示例部分,增加了一个叫Common(含对应的Common.UnitTest)的项目,包括一个XmlHelper的静态类,该类负责提供一般XML操作的支持,同时为了提高执行效率他内部包括了一个自主维护的缓冲机制。该类会随着后续各模式的使用要求不断充实。

//C#
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Xsl;
using System.IO;
using System.Collections.Generic;
namespace VisionTask.Training.ServicePattern.Common.Xml
{
/// 完成一般XML 管理的Helper 类型。
public static class XmlHelper
{
/// 保存所有被加载并编译的XSLT 实例缓冲
private static Dictionary<string, XslCompiledTransform> transforms =
new Dictionary<string, XslCompiledTransform>();
/// 保存所有被加载并编译的XSD 实例缓冲
private static XmlSchemaSet schemas = new XmlSchemaSet();

/// 基于XSD 验证目标XML 文件。
public static bool ValidateSchema(string xsdFile,
string targetNamespace, string xmlFile)
{
if (string.IsNullOrEmpty(xsdFile) || string.IsNullOrEmpty(targetNamespace)
|| string.IsNullOrEmpty(xmlFile)) throw new ArgumentException();
LoadSchema(xsdFile, targetNamespace);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = schemas;
try
{
using (XmlReader reader = XmlReader.Create(xmlFile, settings))
while (reader.Read()) ;
}
catch { return false; } //解析过程中出现错误。
return true;
}

/// 根据XSLT 的定义完成XML 文件的转换。
public static void Transform(string xsltFile,
string sourceXmlFile, string targetXmlFile)
{
if (string.IsNullOrEmpty(xsltFile) || string.IsNullOrEmpty(sourceXmlFile)
|| string.IsNullOrEmpty(targetXmlFile)) throw new ArgumentException();
GetTransform(xsltFile).Transform(sourceXmlFile, targetXmlFile);
}

public static XmlDocument Transform(string xsltFile, XmlDocument source)
{
if (string.IsNullOrEmpty(xsltFile)) throw new ArgumentException("xsltFile");
if ((source == null) || (source.DocumentElement == null)) return null;
XslCompiledTransform tranform = GetTransform(xsltFile);
MemoryStream stream = new MemoryStream();
tranform.Transform(source, null, stream);
stream.Position = 0;
XmlDocument target = new XmlDocument();
target.Load(stream);
return target;
}

// helper method
private static XslCompiledTransform GetTransform(string xsltFile)
{
// 根据缓冲情况获取XSLT 实例
XslCompiledTransform transform;
if (!transforms.TryGetValue(xsltFile, out transform))
{
transform = new XslCompiledTransform();
transform.Load(xsltFile);
transforms.Add(xsltFile, transform);
}
return transform;
}

// helper method
private static void LoadSchema(string xsdFile, string targetNamespace)
{
// 根据缓冲情况获取XSD 实例
XmlSchema schema;
if (!schemas.Contains(targetNamespace))
{
schema = new XmlSchema();
schema.SourceUri = xsdFile;
schemas.Add(targetNamespace, xsdFile);
}
}
}
}
//Unit Test
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using VisionTask.Training.ServicePattern.Common.Xml;
namespace Common.UnitTest
{
[TestClass()]
public class XmlHelperTest
{
private const string targetNamespace = "http://www.visionlogic.com/trade";

[TestMethod]
public void TestValidateSchema()
{
Assert.IsTrue(XmlHelper.ValidateSchema("quote.xsd",
targetNamespace, "quote.xml"));
}

[TestMethod]
public void TestTransformWithFileName()
{
string source = "Quote.xml";
string target = "QuoteTarget.xml";
XmlHelper.Transform("Quote.xslt", source, target);
XmlDocument doc = new XmlDocument();
doc.Load(target);
Assert.AreEqual<string>("Quote", doc.DocumentElement.Name);
}

[TestMethod]
public void TransformTestWithXmlDocument()
{
XmlDocument source = new XmlDocument();
source.Load("Quote.xml");
XmlDocument target = XmlHelper.Transform("Quote.xslt", source);
Assert.IsNotNull(target);
Assert.AreEqual<string>("Quote", target.DocumentElement.Name);
}
}
}

 

0
相关文章