简体   繁体   English

XslCompiledTransform和序列化

[英]XslCompiledTransform and Serialization

I am trying to implement some functions that will convert one object to another with XslCompiledTransform. 我正在尝试实现一些功能,这些功能将使用XslCompiledTransform将一个对象转换为另一个对象。

I found some implementations for Serializing an object to XML string and DeSerialize the XML string to an object. 我发现了一些将对象序列化为XML字符串并将XML字符串反序列化为对象的实现。

Another function does the XslCompiledTransform from object1 to obejbct2. 另一个函数执行从object1到obejbct2的XslCompiledTransform。

To generate the XSLT file i used the Altova MapForce, just loaded the XML of the serialized objects and mapped some attributes. 为了生成XSLT文件,我使用了Altova MapForce,仅加载了序列化对象的XML并映射了一些属性。

Now for the problems: 现在针对问题:

  • first I noticed that the XslCompiledTransform doesn't work with XSLT version 2.0. 首先,我注意到XslCompiledTransform不适用于XSLT 2.0版。 is there any newer functions that do work with XSLT 2.0? XSLT 2.0可以使用任何更新的功能吗? maybe some settings? 也许一些设置?
  • secondly I get an exception when trying to DeSerialize the XML to an object: "There was an error deserializing the object of type myObject Input string was not in a correct format." 其次,在尝试将XML反序列化为对象时出现异常:“在反序列化myObject输入字符串类型的对象时出现错误,格式不正确。” I don't understand where is the problem. 我不明白问题出在哪里。 Does anybody have a sample code that does such a thing? 有人有做这种事情的示例代码吗? all I find in google are Transformations of HTML code and not objects. 我在Google中发现的所有内容都是HTML代码的转换,而不是对象。

Here are the functions: 功能如下:

private static string runXSLT(string xsltFile, string inputXML)
        {
            XmlDocument XmlDoc = new XmlDocument();

            // Load the style sheet.
            XslCompiledTransform xslt = new XslCompiledTransform(true);
            xslt.Load(xsltFile);

            StringReader StrReader = new StringReader(inputXML);
            XmlTextReader XmlReader = new XmlTextReader(StrReader);


            //Create an XmlTextWriter which outputs to memory stream
            Stream stream = new MemoryStream();
            XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8);

            // Execute the transform and output the results to a file.
            xslt.Transform(XmlReader, writer);

            stream.Position = 0;
            XmlDoc.Load(stream);
            return XmlDoc.InnerXml;
        }

public static string SerializeAnObject(object AnObject)
        {
            XmlDocument XmlDoc = new XmlDocument();
            DataContractSerializer xmlDataContractSerializer = new DataContractSerializer(AnObject.GetType());
            MemoryStream MemStream = new MemoryStream();
            try
            {
                xmlDataContractSerializer.WriteObject(MemStream, AnObject);
                MemStream.Position = 0;
                XmlDoc.Load(MemStream);
                return XmlDoc.InnerXml;
            }
            finally
            {
                MemStream.Close();
            }

        }

    public static Object DeSerializeAnObject(string XmlOfAnObject, Type ObjectType)
    {
        StringReader StrReader = new StringReader(XmlOfAnObject);
        DataContractSerializer xmlDataContractSerializer = new DataContractSerializer(ObjectType);
        XmlTextReader XmlReader = new XmlTextReader(StrReader);
        try
        {
            Object AnObject = xmlDataContractSerializer.ReadObject(XmlReader);
            return AnObject;
        }
        finally
        {
            XmlReader.Close();
            StrReader.Close();
        }
    }

Thanks allot, 感谢分配,

Omri. 暗利。

  • XslCompiledTransform does not support XSLT 2.0. XslCompiledTransform不支持XSLT 2.0。 In fact, XSLT 2.0 is not supported within the .NET Framework at all (you could try the Saxon version for .NET, but be aware that this is just the Java version running inside IKVM). 实际上,.NET Framework根本不支持XSLT 2.0(您可以尝试.NET的Saxon版本,但是请注意,这只是IKVM中运行的Java版本)。

  • From your description I did not understand why you are taking the detour via XML to convert one object into another. 从您的描述中,我不明白您为什么要通过XML绕道而行,将一个对象转换为另一个对象。 Why don't you simply provide a constructor in your target object that takes your input object as a paramater? 为什么不简单地在目标对象中提供一个将输入对象作为参数的构造函数呢? Then you can code all the mapping inside that constructor. 然后,您可以在该构造函数中编写所有映射。 This is not onlyby far more efficient than serializing, transforming and deserializing your objects you will also get the type safety of C#. 这不仅比对对象进行序列化,转换和反序列化更为有效,而且还可以获得C#的类型安全性。

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

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