我正在使用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