Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle xml that contains nested xml using c# xmlreader?
    primarykey
    data
    text
    <p>I'm using c# to interact with a database that has an exposed REST API. The table that I'm interested in contains forum posts, some of which themselves contain xml.</p> <p>Whenever my result set contains a post that has xml, my application throws an error as follows:</p> <p><em>Exception Details: System.Xml.XmlException: '>' is an unexpected token. The expected token is '"' or '''. Line 1, position 62.</em></p> <p>And this is the line that fails:</p> <p><em>Line 44: ds.ReadXml(xmlData);</em></p> <p>And this is the code I'm using:</p> <pre><code> var webClient = new WebClient(); string searchString = searchValue.Text; string requestUrl = "http://myserver/restapi.ashx/search.xml?pagesize=4&amp;pageindex=0&amp;query="; requestUrl += searchString; XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; XmlReader xmlData = XmlReader.Create(webClient.OpenRead(requestUrl),settings); DataSet ds = new DataSet(); ds.ReadXml(xmlData); Repeater1.DataSource = ds.Tables[1]; Repeater1.DataBind(); </code></pre> <p>And this is the type of XML record that it's choking on (the stuff in the node is causing the problem):</p> <pre><code> &lt;SearchResults PageSize="1" PageIndex="0" TotalCount="342"&gt; &lt;SearchResult&gt; &lt;ContentId&gt;994&lt;/ContentId&gt; &lt;Title&gt;Help Files: What are they written in?&lt;/Title&gt; &lt;Url&gt;http://myserver/linktest.aspx&lt;/Url&gt; &lt;Date&gt;2008-10-16T16:18:00+01:00&lt;/Date&gt;&lt;ContentType&gt;post&lt;/ContentType&gt; &lt;Body&gt;&lt;div class="ForumPostBodyArea"&gt; &lt;div class="ForumPostContentText"&gt; &lt;p&gt;Can anyone see anything obviously wrong with this xml, when its fired to CRM Its creating 13 null records.&lt;/p&gt; &lt;p&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&amp;lt;soap:Envelope xmlns:typens="&lt;a href="http://tempuri.org/type"&gt;http://tempuri.org/type&lt;/a&gt;" soap:encodingStyle="&lt;a href="http://schemas.xmlsoap.org/soap/encoding/"&gt;http://schemas.xmlsoap.org/soap/encoding/&lt;/a&gt;" xmlns:soap="&lt;a href="http://schemas.xmlsoap.org/soap/envelope/"&gt;http://schemas.xmlsoap.org/soap/envelope/&lt;/a&gt;" xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSchema-instance"&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/a&gt;" xmlns:soapenc="&lt;a href="http://schemas.xmlsoap.org/soap/encoding/"&gt;http://schemas.xmlsoap.org/soap/encoding/&lt;/a&gt;" xmlns:wsdlns="&lt;a href="http://tempuri.org/wsdl/"&gt;http://tempuri.org/wsdl/&lt;/a&gt;" xmlns:xsd="&lt;a href="http://www.w3.org/2001/XMLSchema%22%3E%3Csoap:Header%3E%3CSessionHeader%3E%3CsessionId"&gt;http://www.w3.org/2001/XMLSchema"&amp;gt;&amp;lt;soap:Header&amp;gt;&amp;lt;SessionHeader&amp;gt;&amp;lt;sessionId&lt;/a&gt; xsi:type="xsd:long"&amp;gt;18208442035524&amp;lt;/sessionId&amp;gt;&amp;lt;/SessionHeader&amp;gt;&amp;lt;/soap:Header&amp;gt;&amp;lt;soap:Body&amp;gt;&amp;lt;typens:add&amp;gt;&amp;lt;entityname xsi:type="xsd:string"&amp;gt;lead&amp;lt;/entityname&amp;gt;&amp;lt;records xsi:nil="true" xsi:type="typens:ewarebase" /&amp;gt;&amp;lt;status xsi:type="xsd:string"&amp;gt;PreRegistration&amp;lt;/status&amp;gt;&amp;lt;requester xsi:type="xsd:string"&amp;gt;Mimnagh&amp;lt;/requester&amp;gt;&amp;lt;personfirstname xsi:type="xsd:string"&amp;gt;Sean&amp;lt;/personfirstname&amp;gt;&amp;lt;personlastname xsi:type="xsd:string"&amp;gt;Test2&amp;lt;/personlastname&amp;gt;&amp;lt;personsalutation xsi:type="xsd:string"&amp;gt;Mr&amp;lt;/personsalutation&amp;gt;&amp;lt;details xsi:type="xsd:string"&amp;gt;test project details&amp;lt;/details&amp;gt;&amp;lt;description xsi:type="xsd:string"&amp;gt;test description details&amp;lt;/description&amp;gt;&amp;lt;comments xsi:type="xsd:string"&amp;gt;test project comments&amp;lt;/comments&amp;gt;&amp;lt;personemail xsi:type="xsd:string"&amp;gt;smimnagh@mac.com&amp;lt;/personemail&amp;gt;&amp;lt;personphonenumber xsi:type="xsd:string"&amp;gt;12334566777&amp;lt;/personphonenumber&amp;gt;&amp;lt;type xsi:type="xsd:string"&amp;gt;PreReg&amp;lt;/type&amp;gt;&amp;lt;companyname xsi:type="xsd:string"&amp;gt;Site Client&amp;lt;/companyname&amp;gt;&amp;lt;/typens:add&amp;gt;&amp;lt;/soap:Body&amp;gt;&amp;lt;/soap:Envelope&amp;gt;&lt;/p&gt; &lt;p&gt;Many thanks&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/Body&gt; &lt;Tags&gt; &lt;Tag&gt;xml&lt;/Tag&gt; &lt;/Tags&gt; &lt;IndexedAt&gt;2010-07-08T11:53:46.848+01:00&lt;/IndexedAt&gt; &lt;/SearchResult&gt; &lt;/SearchResults&gt; </code></pre> <p>Is there something that I can do with the xmlreader to make it ignore whatever's causing the problem?</p> <p>Please note that I can't change the XML prior to consuming it - so if it's malformed then I wonder if there's a way to ignore or modify that particular record without generating an error?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload