views:

57

answers:

2

Hi

I need to a parse an xml string(.NET, C#) which , unfortunately, is not well formed.. the xml stream that i am getting back is

<fOpen>true</fOpen>
<ixBugParent>0</ixBugParent>
<sLatestTextSummary></sLatestTextSummary>
<sProject>Vantive</sProject>
<ixArea>9</ixArea>

I have tried using a xml reader, but its crashing out because it thinks ,and rightfully so, there are 2 node elements wheneever it tries to parse

Is there something that I can do with this ? I cant change the XML, cause I have no control of the code that sends the XML back ..

Any help, would be appreciated.

Thanks and Regards

Gagan Janjua

+1  A: 

What you are getting back is a well-formed XML fragment but as you pointed out, not a well-formed XML document. Can you

  • wrap a top-level element around the returned elements? or
  • reference the returned XML fragment as an external entity from within a shell XML document, and pass the shell document to the XML reader?
LarsH
Yes, i did that @ larsH ..I had to wrap a top level argument around the retuened elements.. that thought was always there ... but dont u think, if i do that and read the string back it will add to the overhead?? since i had to do the code, in production, i had to do it even though i think, it might not be an elegant solution.. your say ????
Gagan
@user412628: Yes, it will add a little overhead. If you're parsing the XML in .NET, I think it would be more elegant to use the XmlParserContext as @Michael suggested.
LarsH
+6  A: 

I think you can use the XmlParserContext in one of the XmlTextReader overloads to specify that the node type is an XmlNodeType.Element, similar to this example from MSDN (http://msdn.microsoft.com/en-us/library/cakk7ha0.aspx):

XmlTextReader tr = new XmlTextReader("<element1> abc </element1> 
  <element2> qrt </element2>
  <?pi asldfjsd ?>
  <!-- comment -->", XmlNodeType.Element, null);

while(tr.Read()) {
    Console.WriteLine("NodeType: {0} NodeName: {1}", tr.NodeType, tr.Name);
    }
Michael Burr
This would be easier than the answer I proposed.
LarsH