Can ElementTree be told to preserve the order of attributes?

With help from @bobince’s answer and these two (setting attribute order, overriding module methods) I managed to get this monkey patched it’s dirty and I’d suggest using another module that better handles this scenario but when that isn’t a possibility: # ======================================================================= # Monkey patch ElementTree import xml.etree.ElementTree as ET def _serialize_xml(write, elem, encoding, qnames, … Read more

How to convert string to XML object in JavaScript?

Non-jQuery version: var parseXml; if (window.DOMParser) { parseXml = function(xmlStr) { return ( new window.DOMParser() ).parseFromString(xmlStr, “text/xml”); }; } else if (typeof window.ActiveXObject != “undefined” && new window.ActiveXObject(“Microsoft.XMLDOM”)) { parseXml = function(xmlStr) { var xmlDoc = new window.ActiveXObject(“Microsoft.XMLDOM”); xmlDoc.async = “false”; xmlDoc.loadXML(xmlStr); return xmlDoc; }; } else { parseXml = function() { return null; } … Read more

How to Read XML in .NET?

Load the XML into an XmlDocument and then use xpath queries to extract the data you need. For example XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlstring); XmlNode errorNode = doc.DocumentElement.SelectSingleNode(“/DataChunk/ResponseChunk/Errors/error”); string errorCode = errorNode.Attributes[“code”].Value; string errorMessage = errorNode.InnerText; If there is potential for the XML having multiple error elements you can use SelectNodes to get an … Read more

How to display   in XML output

Unless your XML has a DTD which says what   means, you cannot use  . The only reason this works in HTML is because the XHTML DTD defines what it means (for XHTML) or it’s just baked in to the parser (HTML). In general you should not use named character entities in XML because such … Read more

adding XML sub-elements

Have a look to the following example : # Document creation [xml]$xmlDoc = New-Object system.Xml.XmlDocument $xmlDoc.LoadXml(“<?xml version=`”1.0`” encoding=`”utf-8`”?><Racine></Racine>”) # Creation of a node and its text $xmlElt = $xmlDoc.CreateElement(“Machine”) $xmlText = $xmlDoc.CreateTextNode(“Mach1”) $xmlElt.AppendChild($xmlText) # Creation of a sub node $xmlSubElt = $xmlDoc.CreateElement(“Adapters”) $xmlSubText = $xmlDoc.CreateTextNode(“Network”) $xmlSubElt.AppendChild($xmlSubText) $xmlElt.AppendChild($xmlSubElt) # Creation of an attribute in the principal … Read more

Powershell, output xml to screen

I couldn’t get the Community Extensions to work and I don’t really want to have to install something extra anyway. I have found another approach on a Microsoft blog – function WriteXmlToScreen ([xml]$xml) { $StringWriter = New-Object System.IO.StringWriter; $XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter; $XmlWriter.Formatting = “indented”; $xml.WriteTo($XmlWriter); $XmlWriter.Flush(); $StringWriter.Flush(); Write-Output $StringWriter.ToString(); } $xml = [xml]'<root><so><user … Read more

Create HTML Table with SQL FOR XML

select (select p.ProblemType as ‘td’ for xml path(”), type), (select p.Onset as ‘td’ for xml path(”), type), (select p.DiagnosisStatus as ‘td’ for xml path(”), type) from tblProblemList p where p.PatientUnitNumber = @PatientUnitNumber for xml path(‘tr’) To add the header as well you can use union all. select (select ‘Problem’ as th for xml path(”), type), … Read more

How can I serialize internal classes using XmlSerializer?

From Sowmy Srinivasan’s Blog – Serializing internal types using XmlSerializer: Being able to serialize internal types is one of the common requests seen by the XmlSerializer team. It is a reasonable request from people shipping libraries. They do not want to make the XmlSerializer types public just for the sake of the serializer. I recently … Read more