Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6078977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:53:40+00:00 2026-05-23T10:53:40+00:00

I try to create a XML file <?xml version=1.0 encoding=utf-8?> <ebooks count=494> ….. <ebook

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:53:40+00:00Added an answer on May 23, 2026 at 10:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My XML File: <?xml version=1.0 encoding=utf-8?> <SimpleKD> <player name=Tardis> <kills>0</kills> <deaths>0</deaths> </player> </SimpleKD> First
I have an XML file like this: <?xml version=1.0 encoding=utf-8?> <RootElement> <Achild> ..... </Achild>
I have created a custom Button as follows. file : buttoncontrol.xml <?xml version=1.0 encoding=utf-8?>
I try to create a new file inside a JSP and try to save
try { // Create an appending file handler boolean append = true; FileHandler handler
I'm using android 3.0 and I try to create bitmap xml that will create
I've got an XML file which I use to create objects, change the objects,
I am working on a small photo Gallery. I create a xml file and
I have created this php script, to create an xml-file from my database: <?php
I'm trying to save an XML file encoded as UTF-16 with cElementTree. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.