Gagan
Gagan

Reputation: 5656

Need to parse a xml string

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

Upvotes: 4

Views: 1362

Answers (2)

Michael Burr
Michael Burr

Reputation: 340208

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);
    }

Upvotes: 7

LarsH
LarsH

Reputation: 27994

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?

Upvotes: 0

Related Questions