简体   繁体   中英

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

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM