XmlWriter 通过 XmlWriter.WriteAttributeString 引发异常(SetAttribute 标记 -> 无效的 XML 文档)
我尝试创建一个
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XmlWriter.WriteElementString
写入一个元素和一个值。这就像使用:换句话说,它不会让您留在您创建的元素内。因此,
我认为您想要:
不过就我个人而言,我宁愿使用 LINQ to XML 在内存中创建整个内容,除非它太大。这通常是一个更容易使用的 API。
XmlWriter.WriteElementString
writes an element and a value. It's like using:In other words, it doesn't leave you within the element you create. So instead of this:
I think you want:
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.