We have a requirement to convert some large XML files from one messy format to a cleaner version. In the process of doing that I discovered an issue with XElement.
This sequence doesn't work all the time. Trust me. If one of the files was small I'd attach it, but they aren't.
After extensive searching I received an 'AI Overview' that provided a solution that has worked with 2,000 files.Code:Dim MyXElement As XElement MyXElement = XElement.Load("Messy file.xml") Dim ndList As List(Of XNode) = MyXElement.DescendantNodesAndSelf.ToList For x As Integer = ndList.Count - 1 To 0 Step -1 If ndList(x).NodeType = Xml.XmlNodeType.ProcessingInstruction Then ndList.RemoveAt(x) End If Next
The answer is to use XDocument.
Hopefully the search bots will find this post, no one should have to go through what I did to find this answer.Code:Dim s As String = someXE.ToString 'convert XElement to string ' Create XDocument from string Dim xdoc As XDocument = XDocument.Parse(s) ' Find the processing instructions Dim piL As List(Of XProcessingInstruction) piL = xdoc.DescendantNodes().OfType(Of XProcessingInstruction)().ToList ' Remove the processing instructions For Each pi As XProcessingInstruction In piL pi.Remove() Next 'the Root of xdoc is a XElement without XProcessingInstruction
When I phrased the question correctly the AI provided this along with the answer,
I'll mark this resolved.When working with XElement in VB.NET, removing a processing instruction can be tricky because processing instructions (<?xml ... ?>) are not part of the content of an XElement — they are part of the XML document (i.e., XDocument) or sometimes part of an XContainer like the parent of an element.




Reply With Quote