4

这是我今天一直在思考的一个快速问题。

我正在尝试将 .Net 数据集转换为 XML 流,使用内存中的 xsl 文件对其进行转换,然后将结果输出到新的 XML 文件。

这是当前的解决方案:

        string transformXML = @"pathToXslDocument";

        XmlDocument originalXml = new XmlDocument();

        XmlDocument transformedXml = new XmlDocument();

        XslCompiledTransform transformer = new XslCompiledTransform();

        DataSet ds = new DataSet();

        string filepath;

        originalXml.LoadXml(ds.GetXml()); //data loaded prior

        StringBuilder sb = new StringBuilder();

        XmlWriter writer = XmlWriter.Create(sb);

        transformer.Load(transformXML);

        transformer.Transform(originalXml, writer); //no need to select the node

        transformedXml.LoadXml(sb.ToString());

        transformedXml.Save(filepath);

        writer.Close();

这是原始代码:

BufferedStream stream = new BufferedStream(new MemoryStream());

DataSet ds = new DataSet();

da.Fill(ds);

ds.WriteXml(stream);

StreamReader sr = new StreamReader(stream, true);

stream.Position = 0; //I'm not certain if this is necessary, but for the StreamReader to read the text the position must be reset.

XmlReader reader = XmlReader.Create(sr, null);  //Problem is created here, the XmlReader is created with none of the data from the StreamReader

XslCompiledTransform transformer = new XslCompiledTransform();

transformer.Load(@"<path to xsl file>");

transformer.Transform(reader, null, writer); //Exception is thrown here, though the problem originates from the XmlReader.Create(sr, null)

由于某种原因,在transformer.Transform 方法中,阅读器没有根节点,实际上阅读器没有从StreamReader 中读取任何内容。

我的问题是这段代码有什么问题?其次,是否有更好的方法将数据集转换/转换/存储为 XML?

编辑:两个答案都很有帮助,技术上 aku 更接近。但是,在尝试了两种解决方案之后,我倾向于一种更类似于 Longhorn 的解决方案。

4

3 回答 3

7

我不确定,但似乎您在将流传递给 XmlReader 之前没有重置流中的位置。在尝试从中读取之前尝试在流的开头寻找。此外,在您向其中写入一些数据后,可能需要关闭\刷新流。

编辑

刚刚尝试了以下代码,它运行良好:

    BufferedStream stream = new BufferedStream(new MemoryStream());
    stream.Write(Encoding.ASCII.GetBytes("<xml>foo</xml>"), 0, "<xml>foo</xml>".Length);
    stream.Seek(0, SeekOrigin.Begin);
    StreamReader sr = new StreamReader(stream);
    XmlReader reader = XmlReader.Create(sr);
    while (reader.Read())
    {
         Console.WriteLine(reader.Value);
    }
    stream.Close();
于 2008-09-03T01:19:39.800 回答
2

您必须选择根节点。这不使用数据集,但我每天都使用这个函数并且效果很好。

System.Xml.XmlDocument orgDoc = new System.Xml.XmlDocument();
orgDoc.LoadXml(orgXML);

// MUST SELECT THE ROOT NODE
XmlNode transNode = orgDoc.SelectSingleNode("/");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);

System.IO.StringReader stream = new System.IO.StringReader(transformXML);
XmlReader reader = XmlReader.Create(stream);

System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform();
trans.Load(reader);
trans.Transform(transNode, writer);

XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());

return doc;
于 2008-09-03T01:25:58.537 回答
0

请查看并使用..

using (MemoryStream memStream = new MemoryStream())
            {
                memStream.Write(Encoding.UTF8.GetBytes(xmlBody), 0, xmlBody.Length);
                memStream.Seek(0, SeekOrigin.Begin);

                using (StreamReader reader = new StreamReader(memStream))
                {
                    // xml reader setting.
                    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings()
                    {
                        IgnoreComments = true,
                        IgnoreWhitespace = true,

                    };

                    // xml reader create.
                    using (XmlReader xmlReader = XmlReader.Create(reader, xmlReaderSettings))
                    {                           
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LoginInfo));
                        myObject = (LoginInfo)xmlSerializer.Deserialize(xmlReader);

                    }

                }         
            }
于 2015-02-27T04:12:34.057 回答