Notifications
Clear all

[Closed] XML Writter format issue

 PEN

if you run this code you will get an unformatted xml string. IF you comment out the comment out the writeValue line it will be formatted. So how do I write the innerText using Writer and get a formatted xml string?

Or is the illegal xml formatting? If you have InnerText can then not have elements?


StringWriter = dotNetObject "System.IO.StringWriter"
penDotNet.show StringWriter

-- Create XmlWriterSettings and fill info
XmlWriterSettings = dotNetObject "System.Xml.XmlWriterSettings"
XmlWriterSettings.Indent = true
XmlWriterSettings.indentChars="	"
XmlWriterSettings.OmitXmlDeclaration = false
XmlWriterSettings.NewLineOnAttributes = false

-- Create new XmlWriter in the StringWriter with the specified settings
XmlWriter=dotNetClass "system.xml.xmlWriter"
Writer = XmlWriter.Create StringWriter XmlWriterSettings

-- Write the document
Writer.WriteStartElement "XMLUI"
 	Writer.WriteValue "This is a test" --COMMENT and re run
	Writer.WriteStartElement "TAB"
	Writer.WriteAttributeString "Name" "Temp"
	Writer.WriteAttributeString "BackGround" ""
		Writer.WriteStartElement "UI" ""
		Writer.WriteAttributeString "Test" "12345"
-- 	Writer.WriteEndElement() --Don't realy need to set the end element as it does it for you. 
-- Writer.WriteEndElement()

-- Release Xml resources
Writer.Flush()
Writer.Close()
xmlString=StringWriter.ToString()

9 Replies

what does the value “This is a test” have to be? how will you get access to this value?

 PEN

Not sure why that maters Denis, but it could be any value or text depending on all sorts of factors. I’m concerned with the formatting of the XML file and that it is human readable.

 PEN

Don’t know if any one needs this.

Send this an xmlDoc and it will return a formatted string as XML


fn reformatXml xml Writer: =
(
	doFlush=false
	if Writer==unsupplied do 
	(
		StringWriter=dotNetObject "System.IO.StringWriter"
		XmlWriterSettings = dotNetObject "System.Xml.XmlWriterSettings"
		XmlWriterSettings.Indent = true
		XmlWriterSettings.indentChars="	"
		XmlWriterSettings.OmitXmlDeclaration = false
		XmlWriterSettings.NewLineOnAttributes = false
		XmlWriter=dotNetClass "system.xml.xmlWriter"
		Writer = XmlWriter.Create StringWriter XmlWriterSettings
		doFlush=true
	)
	
	case xml.name of
	(
		"#document": () --Do nothing
		"xml": () --Do nothing
		"#comment":
		(
			Writer.WriteComment xml.value
		)
		"#text":
		(
			Writer.writeValue xml.value
		)
		default:
		(
			Writer.WriteStartElement xml.name
			for i = 0 to xml.Attributes.count-1 do
			(
				val=xml.getAttribute xml.Attributes.item[i].name
				Writer.WriteAttributeString xml.Attributes.item[i].name val
			)
		)
	)
	
	for i = 0 to xml.childNodes.count-1 do 
	(
		reformatXml xml.childNodes.item[i] Writer:writer StringWriter:StringWriter
	)
	
	if doFlush do
	(
		Writer.Flush()
		Writer.Close()
	)
	
	StringWriter
)

--Test
xmlDoc=dotNetObject "system.xml.xmlDocument"
xmlStr="<?xml version=\"1.0\" encoding=\"UTF-8\"?>           <!--This is a comment--><XMLUI Name=\"Fred\">
<TAB Name=\"Face\"><UI /><!--This is a Button below-->
					<BUTTON Name=\"Reset\" /><INNERTEXT>This is Inner Text</INNERTEXT></TAB></XMLUI>
"
xmlDoc.loadXml xmlStr
clearListener()
xmlStr=reformatXml xmlDoc
xmlStr.toString()

Hello Paul

When I wrote a preset system that held xml data in an attribute parameter block, I used LINQ to get a similar result. The tostring() method of an xdocument returns a formatted xml string.


xdoc = (dotNetclass "System.Xml.Linq.XDocument").Parse xmlStr
xdoc.ToString()

However, it does remove the xml encoding string at the start, so if you still needed that, you could do something like this :

(dotnetobject "system.xml.linq.XDeclaration" "1.0" "utf-8" "").tostring() + "
" + xdoc.ToString()
 PEN

Thanks Pete. Hadn’t seen that but I do want all of it in tact. I will have a look what you have.

 PEN
dotNetClass "System.Xml.Linq.XDocument"

Returns undefined

 PEN

Never mind had to load the assembly:(

 PEN

Oh wow, after doing that I noticed mine had an error in it as well:S

Yours is a whole lot easier too.

 PEN

Not quite still. not I’m back to not having the correct five space Tab character as it is in the Max script editor. Any ideas how to set that?