我正在使用C#中的Webservice,它返回XML格式,如下面..
<ArrayOfLoanReportViewModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/App.ViewModels">
<LoanReportViewModel>
<AccessionNumber>00106</AccessionNumber>
<Amount>2.00</Amount>
<Author>DeSerio ,Tasha</Author>
<BalanceDays>2</BalanceDays>
<BalanceLimit>1</BalanceLimit>
<Charges>1.00</Charges>
<CurrentLoans>2</CurrentLoans>
<Email>none@yahoo.com.my</Email>
<Expiry>22/08/2018 00:00:00</Expiry>
<Isbn>9781600854316</Isbn>
<LoanLimit>3</LoanLimit>
<MemberId>30712</MemberId>
<Name>SUHAINA BT OMAR</Name>
<RenewLimit>1</RenewLimit>
<ReserveLimit>1</ReserveLimit>
<Title>
Salad for Dinner: Simple Recipes for Salads that Make a Meal
</Title>
<loanDueDate>14/06/2015</loanDueDate>
<loanStartDate>08/06/2015</loanStartDate>
</LoanReportViewModel>
<LoanReportViewModel>
<AccessionNumber>0000159542</AccessionNumber>
<Amount>4.00</Amount>
<Author i:nil="true"/>
<BalanceDays>4</BalanceDays>
<BalanceLimit>1</BalanceLimit>
<Charges>1.00</Charges>
<CurrentLoans>2</CurrentLoans>
<Email>none@yahoo.com.my</Email>
<Expiry>22/08/2018 00:00:00</Expiry>
<Isbn>9789675415692</Isbn>
<LoanLimit>3</LoanLimit>
<MemberId>30712</MemberId>
<Name>SUHAINA BT OMAR</Name>
<RenewLimit>1</RenewLimit>
<ReserveLimit>1</ReserveLimit>
<Title>
Setitis rasa dalam Alunan Kata Koleksi Puisi Program Maraton Pembudayaan 1Malaysia Penyusun: Nor Suhaila Che Pa, Suhaidah Said, Siti Nurul Jannah Fital
</Title>
<loanDueDate>12/06/2015</loanDueDate>
<loanStartDate>08/06/2015</loanStartDate>
</LoanReportViewModel>
<LoanReportViewModel>
<AccessionNumber>00105</AccessionNumber>
<Amount>4.00</Amount>
<Author>DeSerio ,Tasha</Author>
<BalanceDays>4</BalanceDays>
<BalanceLimit>2</BalanceLimit>
<Charges>1.00</Charges>
<CurrentLoans>1</CurrentLoans>
<Email>none@yahoo.com</Email>
<Expiry>01/08/2020 00:00:00</Expiry>
<Isbn>9781600854316</Isbn>
<LoanLimit>3</LoanLimit>
<MemberId>10301</MemberId>
<Name>HAINI BINTI MOHD SALLEH</Name>
<RenewLimit>1</RenewLimit>
<ReserveLimit>1</ReserveLimit>
<Title>
Salad for Dinner: Simple Recipes for Salads that Make a Meal
</Title>
<loanDueDate>12/06/2015</loanDueDate>
<loanStartDate>05/06/2015</loanStartDate>
</LoanReportViewModel>
<LoanReportViewModel>
<AccessionNumber>0000157728</AccessionNumber>
<Amount>3.00</Amount>
<Author>ARIFF Raihan</Author>
<BalanceDays>6</BalanceDays>
<BalanceLimit>2</BalanceLimit>
<Charges>0.50</Charges>
<CurrentLoans>1</CurrentLoans>
<Email/>
<Expiry>01/07/2008 00:00:00</Expiry>
<Isbn>9789673640584</Isbn>
<LoanLimit>3</LoanLimit>
<MemberId>HPYM08641S</MemberId>
<Name>TAN AI WEI</Name>
<RenewLimit>1</RenewLimit>
<ReserveLimit>1</ReserveLimit>
<Title>
Kisah Puteri Rupawan Penulis: Ariff Raihan ; Ilustrator: Adeeb
</Title>
<loanDueDate>10/06/2015</loanDueDate>
<loanStartDate>08/06/2015</loanStartDate>
</LoanReportViewModel>
<LoanReportViewModel>
<AccessionNumber>100100100</AccessionNumber>
<Amount>0.00</Amount>
<Author>Karen Ansel</Author>
<BalanceDays>7</BalanceDays>
<BalanceLimit>1</BalanceLimit>
<Charges>0.00</Charges>
<CurrentLoans>2</CurrentLoans>
<Email>nana@gmail.com</Email>
<Expiry>09/04/2016 00:00:00</Expiry>
<Isbn>9781740899802</Isbn>
<LoanLimit>3</LoanLimit>
<MemberId>10310</MemberId>
<Name>Nurul Farhana Mohd Fakeh</Name>
<RenewLimit>10</RenewLimit>
<ReserveLimit>10</ReserveLimit>
<Title>
The Baby and Toddler Cookbook: Fresh, Homemade Foods for a Healthy Start
</Title>
<loanDueDate>09/06/2015</loanDueDate>
<loanStartDate>05/06/2015</loanStartDate>
</LoanReportViewModel>
<LoanReportViewModel>
<AccessionNumber>0000146757</AccessionNumber>
<Amount>0.00</Amount>
<Author>Siti Rosmizah 1972-</Author>
<BalanceDays>17</BalanceDays>
<BalanceLimit>1</BalanceLimit>
<Charges>0.00</Charges>
<CurrentLoans>2</CurrentLoans>
<Email>nana@gmail.com</Email>
<Expiry>09/04/2016 00:00:00</Expiry>
<Isbn>9789675822018</Isbn>
<LoanLimit>3</LoanLimit>
<MemberId>10310</MemberId>
<Name>Nurul Farhana Mohd Fakeh</Name>
<RenewLimit>10</RenewLimit>
<ReserveLimit>10</ReserveLimit>
<Title>7 hari mencintaiku Siti Rosmizah</Title>
<loanDueDate>30/05/2015</loanDueDate>
<loanStartDate>28/05/2015</loanStartDate>
</LoanReportViewModel>
<LoanReportViewModel>
<AccessionNumber>0000159109</AccessionNumber>
<Amount>24.00</Amount>
<Author>ROSE Eliza</Author>
<BalanceDays>24</BalanceDays>
<BalanceLimit>2</BalanceLimit>
<Charges>1.00</Charges>
<CurrentLoans>1</CurrentLoans>
<Email>yaya@yahoo.com</Email>
<Expiry>09/04/2016 00:00:00</Expiry>
<Isbn>9789670535654</Isbn>
<LoanLimit>3</LoanLimit>
<MemberId>10210</MemberId>
<Name>Nurul Farehah Mohd Sabri</Name>
<RenewLimit>1</RenewLimit>
<ReserveLimit>1</ReserveLimit>
<Title>Sayang Awak Mr.Bear Rose Eliza</Title>
<loanDueDate>23/05/2015</loanDueDate>
<loanStartDate>19/05/2015</loanStartDate>
</LoanReportViewModel>
</ArrayOfLoanReportViewModel>
这是我的代码:
WebRequest request = HttpWebRequest.Create(url);
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
stream.Flush();
stream.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(stream);
}
}
运行代码后,我在Doc.Load中遇到异常 - “根级别的数据无效。第1行,第1位“解析XML时..
如何克服这个错误?我尝试了所有的方法。因为XML initail标头格式错误。我收到这个错误,如何克服这个问题?
答案 0 :(得分:0)
如果您发布的XML是您收到的整个XML,则它缺少根级别元素。尝试使用XmlDocumentFragment
像这样的东西应该有效:
var doc = new XmlDocument();
var xml = doc.CreateDocumentFragment();
xml.InnerXml = stream;
答案 1 :(得分:0)
最后我解决了这个问题。对于遇到此问题的其他人有用。
工作代码:
WebRequest request = WebRequest.Create(url);
// If required by the server, set the credentials.r
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/xml; charset=utf-8";
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//// Display the status.
//Console.WriteLine(response.StatusDescription);
//// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(responseFromServer));
if (ds.Tables[0].Rows.Count != 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strArrNames.Add(ds.Tables[0].Rows[i]["Name"].ToString());
strArrEmails.Add(ds.Tables[0].Rows[i]["Email"].ToString());
strArrTitles.Add(ds.Tables[0].Rows[i]["Title"].ToString());
strArrBalanceDays.Add(ds.Tables[0].Rows[i]["BalanceDays"].ToString());
strArrLoanDueDates.Add(ds.Tables[0].Rows[i]["loanDueDate"].ToString());
}
overduestatus = true;
}
谢谢大家..