XmlWriter 通过 XmlWriter.WriteAttributeString 引发异常(SetAttribute 标记 -> 无效的 XML 文档)

发布于 2024-11-17 16:44:41 字数 2561 浏览 3 评论 0原文

我尝试创建一个

<?xml version="1.0" encoding="utf-8"?>
<ebooks count="494">
.....
<ebook absolute_path="J:\eBooks\Apress - Pro ASP.NET 4 in VB 2010, Third Edition.Sep.2010.ENG.pdf">
    <file>Apress - Pro ASP.NET 4 in VB 2010, Third Edition.Sep.2010.ENG.pdf</file>
    <size bytes="40176052">40Mb</size>
    <created datetime="25.12.2010 12:48:52">25.12.2010</created>
    <location>J:\eBooks</location>
</ebook>
<ebook absolute_path="J:\eBooks\Apress - Pro PHP and jQuery.Jun.2010.pdf">
    <file>Apress - Pro PHP and jQuery.Jun.2010.pdf</file>
    <size bytes="12523132">12.5Mb</size>
    <created datetime="10.07.2010 19:43:10">10.07.2010</created>
    <location>J:\eBooks</location>
  </ebook>
.....
</ebooks>

按照 VB.NET 代码编写的 XML 文件来实现该输出,但我总是收到有关 SetAttribute 令牌和无效 XML 文档的异常。

Dim xmlFileName As String = "J:\eBooks\report_" & DateAndTime.Now.ToShortDateString & ".xml"
Dim xmlSetting As XmlWriterSettings = New XmlWriterSettings()
xmlSetting.Indent = True
Dim xw As XmlWriter = XmlWriter.Create(xmlFileName, xmlSetting)
...
...
dim singleFile as String
Dim myPdfFiles() as String = Directory.GetFiles(<path_to_dir>, <pdf_files>, SearchOption.AllDirectories)
Try
    xw.WriteStartDocument()
    xw.WriteStartElement("ebooks")
    xw.WriteAttributeString("count", myPdfFiles.Count.ToString)
    For Each singleFile In myPdfFiles
        Dim fi As New FileInfo(singleFile)
        With xw
            .WriteStartElement("ebook")
            .WriteAttributeString("absolute_path", fi.FullName.ToString)
            .WriteElementString("file", fi.Name.ToString)
            .WriteElementString("size", Math.Round(fi.Length / 1048576, 2) & "Mb")
            'Attribute BYTES for <size>....'
            .WriteAttributeString("bytes", fi.Length.ToString) '<- EXCEPTION!!!!'
            .WriteElementString("created", fi.CreationTime.ToShortDateString)
            'xw.WriteAttributeString("datetime", fi.CreationTime) <- would throw exception too'
            .WriteElementString("location", fi.DirectoryName)
            .WriteEndElement() '...close <ebook> element'
        End With
     Next
     xw.WriteEndElement() '..close <ebooks> element'
     xw.WriteEndDocument()
     xw.Flush()
     xw.Close()
Catch ex As Exception
    MsgBox("Message : " & ex.Message)
End Try

有什么想法为什么我会得到例外吗?如何获得上述 XML 输出?

I try to create a XML file

<?xml version="1.0" encoding="utf-8"?>
<ebooks count="494">
.....
<ebook absolute_path="J:\eBooks\Apress - Pro ASP.NET 4 in VB 2010, Third Edition.Sep.2010.ENG.pdf">
    <file>Apress - Pro ASP.NET 4 in VB 2010, Third Edition.Sep.2010.ENG.pdf</file>
    <size bytes="40176052">40Mb</size>
    <created datetime="25.12.2010 12:48:52">25.12.2010</created>
    <location>J:\eBooks</location>
</ebook>
<ebook absolute_path="J:\eBooks\Apress - Pro PHP and jQuery.Jun.2010.pdf">
    <file>Apress - Pro PHP and jQuery.Jun.2010.pdf</file>
    <size bytes="12523132">12.5Mb</size>
    <created datetime="10.07.2010 19:43:10">10.07.2010</created>
    <location>J:\eBooks</location>
  </ebook>
.....
</ebooks>

I wrote following VB.NET code to achieve that output, but I always get an exception about SetAttribute Token and unvalid XML document.

Dim xmlFileName As String = "J:\eBooks\report_" & DateAndTime.Now.ToShortDateString & ".xml"
Dim xmlSetting As XmlWriterSettings = New XmlWriterSettings()
xmlSetting.Indent = True
Dim xw As XmlWriter = XmlWriter.Create(xmlFileName, xmlSetting)
...
...
dim singleFile as String
Dim myPdfFiles() as String = Directory.GetFiles(<path_to_dir>, <pdf_files>, SearchOption.AllDirectories)
Try
    xw.WriteStartDocument()
    xw.WriteStartElement("ebooks")
    xw.WriteAttributeString("count", myPdfFiles.Count.ToString)
    For Each singleFile In myPdfFiles
        Dim fi As New FileInfo(singleFile)
        With xw
            .WriteStartElement("ebook")
            .WriteAttributeString("absolute_path", fi.FullName.ToString)
            .WriteElementString("file", fi.Name.ToString)
            .WriteElementString("size", Math.Round(fi.Length / 1048576, 2) & "Mb")
            'Attribute BYTES for <size>....'
            .WriteAttributeString("bytes", fi.Length.ToString) '<- EXCEPTION!!!!'
            .WriteElementString("created", fi.CreationTime.ToShortDateString)
            'xw.WriteAttributeString("datetime", fi.CreationTime) <- would throw exception too'
            .WriteElementString("location", fi.DirectoryName)
            .WriteEndElement() '...close <ebook> element'
        End With
     Next
     xw.WriteEndElement() '..close <ebooks> element'
     xw.WriteEndDocument()
     xw.Flush()
     xw.Close()
Catch ex As Exception
    MsgBox("Message : " & ex.Message)
End Try

Any Ideas why I get an exception? How do I get the XML-output as above ?

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

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

发布评论

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

评论(1

半衬遮猫 2024-11-24 16:44:41

XmlWriter.WriteElementString 写入一个元素和一个值。这就像使用:

writer.WriteStartElement("name");
writer.WriteString("value");
writer.WriteEndElement();

换句话说,它不会让您留在您创建的元素内。因此,

.WriteElementString("size", Math.Round(fi.Length / 1048576, 2) & "Mb")
.WriteAttributeString("bytes", fi.Length.ToString)

我认为您想要:

.WriteStartElement("size")
.WriteAttributeString("bytes", fi.Length.ToString)
.WriteString(Math.Round(fi.Length / 1048576, 2) & "Mb")
.WriteEndElement()

不过就我个人而言,我宁愿使用 LINQ to XML 在内存中创建整个内容,除非它太大。这通常是一个更容易使用的 API。

XmlWriter.WriteElementString writes an element and a value. It's like using:

writer.WriteStartElement("name");
writer.WriteString("value");
writer.WriteEndElement();

In other words, it doesn't leave you within the element you create. So instead of this:

.WriteElementString("size", Math.Round(fi.Length / 1048576, 2) & "Mb")
.WriteAttributeString("bytes", fi.Length.ToString)

I think you want:

.WriteStartElement("size")
.WriteAttributeString("bytes", fi.Length.ToString)
.WriteString(Math.Round(fi.Length / 1048576, 2) & "Mb")
.WriteEndElement()

Personally though, I'd rather create the whole thing in memory using LINQ to XML, unless it's going to be too big. That's generally an easier API to work with.

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