0

I am trying to extract a value from a specific node on an xml response. On executing I am seeing error "Data at the root level is invalid. Line 1, position 1".

I understand from this Stackflow Question that the solution to this sort of problem is trim special characters however when I attempt the solution provided xmlMsg.Startswith gives a message "'XmlDocument' does not contain a definition for Startswith..."

        XmlDocument xmlMsg = new XmlDocument(); 
        xmlMsg.LoadXml(_restResponse.Content); //------------->FAILS HERE

Am I doing something wrong (clearly I am)? Is there another way to achieve what I am trying to do?

This is using VS 2022 and .net-6.

ZR_
  • 1
  • 1
  • What does the actual XML look like? Particularly line 1. – Kirk Woll Mar 10 '22 at 13:46
  • 2
    That would suggest that `_restResponse.Content` isn't valid XML. However, it's hard to say anything more without seeing what that actually is... – Jon Skeet Mar 10 '22 at 13:47
  • 1
    (As an aside, when you've figured that part out, I'd strongly advise using LINQ to XML (`XDocument` etc) instead of the old `XmlDocument` API - LINQ to XML is *much* easier to work with.) – Jon Skeet Mar 10 '22 at 13:47
  • I'd also note that in the case of the linked question, you'd be calling `StartsWith` on `_restReponse.Content` *not* `xmlMsg`. However, it's a bit of a wild guess of a solution. Anything could be wrong with it - including that it's not XML at all. – Charles Mager Mar 10 '22 at 16:09

0 Answers0