我正在使用c#與具有公開REST API的數據庫進行交互。我感興趣的表格包含論壇帖子,其中一些本身包含xml。如何使用c#xmlreader處理包含嵌套xml的xml?
每當我的結果集包含具有XML後,我的應用程序如下拋出一個錯誤:
異常詳細信息:System.Xml.XmlException:「>」是一個意外標記。預期令牌是 '「' 或 '''行1,位置62
這是失敗的行:
線44:ds.ReadXml(XMLDATA);
。這是我使用的代碼:
var webClient = new WebClient();
string searchString = searchValue.Text;
string requestUrl = "http://myserver/restapi.ashx/search.xml?pagesize=4&pageindex=0&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();
這是XML的記錄,它的窒息(在節點中的東西引起的問題)類型:
<SearchResults PageSize="1" PageIndex="0" TotalCount="342">
<SearchResult>
<ContentId>994</ContentId>
<Title>Help Files: What are they written in?</Title>
<Url>http://myserver/linktest.aspx</Url>
<Date>2008-10-16T16:18:00+01:00</Date><ContentType>post</ContentType>
<Body><div class="ForumPostBodyArea"> <div class="ForumPostContentText"> <p>Can anyone see anything obviously wrong with this xml, when its fired to CRM Its creating 13 null records.</p> <p><?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:typens="<a href="http://tempuri.org/type">http://tempuri.org/type</a>" soap:encodingStyle="<a href="http://schemas.xmlsoap.org/soap/encoding/">http://schemas.xmlsoap.org/soap/encoding/</a>" xmlns:soap="<a href="http://schemas.xmlsoap.org/soap/envelope/">http://schemas.xmlsoap.org/soap/envelope/</a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>" xmlns:soapenc="<a href="http://schemas.xmlsoap.org/soap/encoding/">http://schemas.xmlsoap.org/soap/encoding/</a>" xmlns:wsdlns="<a href="http://tempuri.org/wsdl/">http://tempuri.org/wsdl/</a>" xmlns:xsd="<a href="http://www.w3.org/2001/XMLSchema%22%3E%3Csoap:Header%3E%3CSessionHeader%3E%3CsessionId">http://www.w3.org/2001/XMLSchema"><soap:Header><SessionHeader><sessionId</a> xsi:type="xsd:long">18208442035524</sessionId></SessionHeader></soap:Header><soap:Body><typens:add><entityname xsi:type="xsd:string">lead</entityname><records xsi:nil="true" xsi:type="typens:ewarebase" /><status xsi:type="xsd:string">PreRegistration</status><requester xsi:type="xsd:string">Mimnagh</requester><personfirstname xsi:type="xsd:string">Sean</personfirstname><personlastname xsi:type="xsd:string">Test2</personlastname><personsalutation xsi:type="xsd:string">Mr</personsalutation><details xsi:type="xsd:string">test project details</details><description xsi:type="xsd:string">test description details</description><comments xsi:type="xsd:string">test project comments</comments><personemail xsi:type="xsd:string">[email protected]</personemail><personphonenumber xsi:type="xsd:string">12334566777</personphonenumber><type xsi:type="xsd:string">PreReg</type><companyname xsi:type="xsd:string">Site Client</companyname></typens:add></soap:Body></soap:Envelope></p> <p>Many thanks</p> </div> </div>
</Body>
<Tags>
<Tag>xml</Tag>
</Tags>
<IndexedAt>2010-07-08T11:53:46.848+01:00</IndexedAt>
</SearchResult>
</SearchResults>
有沒有什麼,我可以用xmlreader做,使其忽略任何導致問題?
請注意,在使用XML之前我無法更改XML - 所以如果格式不正確,我不知道是否有辦法忽略或修改該特定記錄而不會產生錯誤?
謝謝!
我無法控制XML,因爲它來自第三方應用程序。所以我需要一些方法來處理它的畸形狀態。 – 2010-07-27 13:07:46