Notifications
Clear all

[Closed] DotNet XML Pretty Print

Hi everyone,

I’m using a customized version of a DotNet XmlDocument wrapper someone made. The problem was that they saved everything as one line, as where I need new lines and indentation.

I’ve searched and researched and have tried implementing solutions others have suggested for formatting dotnet XML, but I’m having a problem with the file I’m writing to not being accessible.

Here’s a bit of code to illustrate the main parts of what I’m doing:

...inside XMLDocument struct...
file = "*desktop*\\Temp.xml"
dom = dotNetObject "System.Xml.XmlDocument",
writer = dotNetClass "System.Xml.XmlWriter",
writerSettings = dotNetObject "System.Xml.XmlWriterSettings",
...
doc = dom.Load file
...
writerSettings.Indent = true
writerSettings.NewLineOnAttributes = true		
writer = writer.Create file writerSettings		
dom.Save writer
...

Running the script gives me this in the listener – Runtime error: dotNet runtime exception: The process cannot access the file etc etc.

Apparently it can’t write back to the file it has open… I tried digging in the XmlDocument API and couldn’t find anything about closing files before saving them.

7 Replies
 lo1

You shouldn’t have to close the file (you couldn’t save it otherwise), the problem must be something else. Perhaps the file is marked as read only?

3 Replies
(@solarnoise)
Joined: 11 months ago

Posts: 0

Yeah, makes sense. I was just wondering if there was an intermediate step needed between loading and saving to keep the file accessible.

The file being saved is a brand new file being created by the script… I can’t imagine there would be any read/write permissions issues… I’m not specifying that the file is read-only when it’s created.

 lo1
(@lo1)
Joined: 11 months ago

Posts: 0

This could be the problem. How are you creating it?

(@solarnoise)
Joined: 11 months ago

Posts: 0

Forgot to answer this… the file is created in the Save function I outlined one post above. Initially the file is a filename string, which gets created during that Save function.

From then on out, I use the Load function found in the XmlDocument object to open it.

I fixed it, guys! And I was RIGHT about needing to close the stream

I just needed to close the writer instance, so now the save function looks like:

writerSettings.Indent = true
			
writer = writer.Create file writerSettings
			
dom.Save writer
writer.Close()

…where “file” is the temp.xml I created on my desktop upon loading the file.

And I’m now able to open the file I just saved, write to it or edit it, and then close it again.

I now just need to figure out how to make the XmlWriter print the text the way I want.

Yeah still having problems with my output formatting. Even with using a XmlWriter with a corresponding XmlWriterSettings object with indent set to “true”, my XML still comes out all on one line.

The settings object has a property for putting attributes on new lines, but I want the opening/closing element tags to be on new lines as well as their children, which I’d like indented. Basically, what you would expect from XML formatting.

Anyone have any ideas? I’ve seen people on stackoverflow suggest using a XmlTextWriter instead, but I’m not sure how nicely that works with my existing model which uses XmlDocument.

Once again I discovered the solution! And am posting here in case anyone comes by this problem and is scratching their heads.

My problem was adding text between nodes. Apparently it messes up the formatting, whether using just XmlDocument, or a combination of XmlWriter, XmlWriterSettings, and XmlTextWriter.

I’m using XmlWriter/Settings again and it’s working fine. Using Flush/Close after saving the file.