XslCompiledTransform 和序列化

发布于 2024-08-02 06:30:19 字数 2571 浏览 6 评论 0原文

我正在尝试实现一些函数,使用 XslCompiledTransform 将一个对象转换为另一个对象。

我找到了一些将对象序列化为 XML 字符串和将 XML 字符串反序列化为对象的实现。

另一个函数执行从 object1 到 obejbct2 的 XslCompiledTransform。

为了生成 XSLT 文件,我使用了 Altova MapForce,仅加载了序列化对象的 XML 并映射了一些属性。

现在解决问题:

  • 首先我注意到 XslCompiledTransform 不适用于 XSLT 版本 2.0。有没有可以与 XSLT 2.0 一起使用的新函数?也许一些设置?
  • 其次,当我尝试将 XML 反序列化为对象时出现异常: “反序列化 myObject 类型的对象时发生错误 输入字符串的格式不正确。” 我不明白问题出在哪里。 有人有执行此类操作的示例代码吗?我在 google 中找到的只是 HTML 代码的转换,而不是对象。

以下是功能:

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();
        }
    }

谢谢分配,

Omri。

I am trying to implement some functions that will convert one object to another with XslCompiledTransform.

I found some implementations for Serializing an object to XML string and DeSerialize the XML string to an object.

Another function does the XslCompiledTransform from object1 to obejbct2.

To generate the XSLT file i used the Altova MapForce, just loaded the XML of the serialized objects and mapped some attributes.

Now for the problems:

  • first I noticed that the XslCompiledTransform doesn't work with XSLT version 2.0. is there any newer functions that do work with 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."
    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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小鸟爱天空丶 2024-08-09 06:30:19
  • XslCompiledTransform 不支持 XSLT 2.0。事实上,.NET Framework 根本不支持 XSLT 2.0(您可以尝试 .NET 的 Saxon 版本,但请注意,这只是 IKVM 内运行的 Java 版本)。

  • 根据您的描述,我不明白您为什么要绕道通过 XML 将一个对象转换为另一个对象。为什么不在目标对象中提供一个构造函数来将输入对象作为参数呢?然后您可以在该构造函数内编写所有映射。这不仅比序列化、转换和反序列化对象更有效,您还将获得 C# 的类型安全性。

  • XslCompiledTransform does not support 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).

  • From your description I did not understand why you are taking the detour via XML to convert one object into another. 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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文