davids
davids

Reputation: 5577

Error parsing XML string to XDocument

I have this XML string bn:

<Root><Row><ITEMNO>1</ITEMNO><USED>y</USED><PARTSOURCE>Buy</PARTSOURCE><QTY>2</QTY></Row><Row><ITEMNO>5</ITEMNO><PARTSOURCE>Buy</PARTSOURCE><QTY>5</QTY></Row></Root>

I am trying to convert it to an XDocument like this:

var doc = XDocument.Parse(bn);

However, I get this error:

Data at the root level is invalid. Line 1, position 1.

Am I missing something?

UPDATE:

This is the method I use to create the xml string:

public static string SerializeObjectToXml(Root rt)
{
    var memoryStream = new MemoryStream();
    var xmlSerializer = new XmlSerializer(typeof(Root));
    var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

    xmlSerializer.Serialize(xmlTextWriter, rt);
    memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

    string xmlString = ByteArrayToStringUtf8(memoryStream.ToArray());

    xmlTextWriter.Close();
    memoryStream.Close();
    memoryStream.Dispose();

    return xmlString;
}

It does add to the start that I have to remove. Could I change something to make it correct from the start?

Upvotes: 0

Views: 5047

Answers (3)

Brian Warshaw
Brian Warshaw

Reputation: 22984

The accepted answer does unnecessary string processing, but, in its defense, it's because you're unnecessarily dealing in string when you don't have to. One of the great things about the .NET XML APIs is that they have robust internals. So instead of trying to feed a string to XDocument.Parse, feed a Stream or some type of TextReader to XDocument.Load. This way, you aren't fooling with manually managing the encoding and any problems it creates, because the internals will handle all of that stuff for you. Byte-order marks are a pain in the neck, but if you're dealing in XML, .NET makes it easier to handle them.

Upvotes: 0

It&#39;sNotALie.
It&#39;sNotALie.

Reputation: 22794

There is two characters at the beginning of your string that, although you can't see them, are still there and make the string fail. Try this instead:

<Root><Row><ITEMNO>1</ITEMNO><USED>y</USED><PARTSOURCE>Buy</PARTSOURCE><QTY>2</QTY></Row><Row><ITEMNO>5</ITEMNO><PARTSOURCE>Buy</PARTSOURCE><QTY>5</QTY></Row></Root>

The character in question is this. This is a byte-order mark, basically telling the program reading it if it's big or little endian. It seems like you copied and pasted this from a file that wasn't decoded properly.

To remove it, you could use this:

yourString.Replace(((char)0xFEFF).ToString(), "")

Upvotes: 2

SLaks
SLaks

Reputation: 887275

You have two unprintable characters (Zero-Width No-break Space) at the beginning of your string.
XML does not allow text outside the root element.

Upvotes: 1

Related Questions