Jordan Ryder
Jordan Ryder

Reputation: 2822

C# Load XML with special characters inside node

We receive an xml string from an external API, and one element has a bunch of GT/LT signs.

When we run this code, it fails:

var xml = @"<SomeNode>10040:<->10110:<->10130:<->10150:<->10160:<->10180:<->10330:Value=><->10330:Matching=><->10330:Value2=><->10330:Value3=><->10330:Value4=><->10447:<->10418:No<->10419:No<->10430:No
</SomeNode>";

var doc = new XmlDocument();
doc.LoadXml(xml);

//System.Xml.XmlException: 'Name cannot begin with the '-' character, hexadecimal value 0x2D

I looked into escaping those characters, but as far as I can tell there isn't a way to escape only the ones inside SomeNode.

So I know that I could run some kind of string replacement using a regex or something to clear that out. But, is there an elegant way to solve this using existing XML related tools?

Upvotes: 1

Views: 605

Answers (1)

Jordan Ryder
Jordan Ryder

Reputation: 2822

Based on the comments, there isn't an xml tools solution, and so it'll be a custom string replacement solution.

Upvotes: 1

Related Questions