XML 转换后保留 DTD

发布于 2024-08-31 21:09:44 字数 1018 浏览 4 评论 0原文

我正在转换一个 XML 文档,但转换后我的 DTD 消失了,而且告诉 XML 版本的第一行也丢失了。

<?xml version="1.0"?>

我用来转换 XML 文件的代码是:

// Load the style sheet.
            var xslt = new XslCompiledTransform();
            xslt.Load("XSLTFile1.xslt");

            // Create the writer.
            var settings = new XmlWriterSettings
                            {
                                Indent = true,
                                IndentChars = "\t",
                                ConformanceLevel = ConformanceLevel.Auto,
                                Encoding = Encoding.UTF8,
                            };

            var mydoc = XDocument.Load("Doc1.xml"); 

            var writer = XmlWriter.Create("Doc2.xml", settings);

            // Execute the transform and output the results to a file.
            if (writer != null)
            {
                xslt.Transform(mydoc.CreateReader(), writer);
                writer.Close();
            }

有什么想法吗?

I am transforming an XML document but after the transform my DTD goes away and also the first line which tells the XML version is missing.

<?xml version="1.0"?>

The code I am using to transform the XML file is:

// Load the style sheet.
            var xslt = new XslCompiledTransform();
            xslt.Load("XSLTFile1.xslt");

            // Create the writer.
            var settings = new XmlWriterSettings
                            {
                                Indent = true,
                                IndentChars = "\t",
                                ConformanceLevel = ConformanceLevel.Auto,
                                Encoding = Encoding.UTF8,
                            };

            var mydoc = XDocument.Load("Doc1.xml"); 

            var writer = XmlWriter.Create("Doc2.xml", settings);

            // Execute the transform and output the results to a file.
            if (writer != null)
            {
                xslt.Transform(mydoc.CreateReader(), writer);
                writer.Close();
            }

Any ideas?

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

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

发布评论

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

评论(2

静赏你的温柔 2024-09-07 21:09:45

为了保留 XML 声明,您需要在 XmlWriterSettings 中确保 OmitXmlDeclaration 设置为 false

var settings = new XmlWriterSettings
                   {
                     Indent = true,
                     IndentChars = "\t",
                     ConformanceLevel = ConformanceLevel.Auto,
                     Encoding = Encoding.UTF8,
                     OmitXmlDeclaration = false,
                    };

至于 DTD“ away” - 由于您正在转换文档,因此您应该向转换后的文档添加新的 DTD 声明。

如果没有 xslxml 文件,就很难确定。您可以编辑您的问题并添加它们吗?

In order to keep the XML declaration, you need to make sure in your XmlWriterSettings that OmitXmlDeclaration is set to false:

var settings = new XmlWriterSettings
                   {
                     Indent = true,
                     IndentChars = "\t",
                     ConformanceLevel = ConformanceLevel.Auto,
                     Encoding = Encoding.UTF8,
                     OmitXmlDeclaration = false,
                    };

As for the DTD "going away" - since you are transforming the document, you should add a new DTD declaration to the transformed document.

Without the xsl and xml files, it is difficult to tell for certain. Can you edit your question and add them?

ㄟ。诗瑗 2024-09-07 21:09:45

嘿,谢谢你的回复。我做到了

writer.WriteDocType(mydoc.DocumentType.Name, mydoc.DocumentType.PublicId, mydoc.DocumentType.SystemId, mydoc.DocumentType.InternalSubset); 

并且成功了。是的,我也有,

OmitXmlDeclaration = false,

谢谢

hey thanks for the reply. I did

writer.WriteDocType(mydoc.DocumentType.Name, mydoc.DocumentType.PublicId, mydoc.DocumentType.SystemId, mydoc.DocumentType.InternalSubset); 

And it worked. Yes I also have

OmitXmlDeclaration = false,

Thanks

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