2013-04-03 91 views
0

我在嘗試解析用C#中的Axis Java Web Service生成的XML文件時遇到了一些問題。該文件的格式如下:如何解析Axis XML文件?

<ns:getAcctsDetailResponse xmlns:ns="http://paymentdata.com"> 
    <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax21="http://paymentdata.com/xsd" xsi:type="ax21:AcctsDetail"> 
    <ax21:Status>15</ax21:Status> 
    <ax21:Name>John James</ax21:Name> 
</ns:return> 
</ns:getCustomerAcctsDetailResponse> 

我用下面的代碼嘗試訪問所需要的元素,但得到以下錯誤:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

XDocument xDoc = XDocument.Load(xml); 
string accountName= xDoc.Root.Element("ns:return").Element("ax21:Name").Value; 

您的幫助將不勝感激。

+1

你說你有點問題 - 這表明你有代碼不工作。請展示它,並說出錯的地方。 – 2013-04-03 22:05:51

+0

您需要處理命名空間('ns:'和'ax21:')部分。查看此答案的示例:http://stackoverflow.com/a/2340497/745969 – Tim 2013-04-03 22:47:00

回答

0

我看到兩個問題:

首先,該XML格式錯誤。 ns:getAcctsDetailResponse開始標記與ns:getCustomerAcctsDetailResponse結束標記不匹配。也許這是一個錯字?

其次,你需要爲命名空間做一些特殊的事情。嘗試這樣的:

XNamespace ns = "http://paymentdata.com"; 
XNamespace ax21 = "http://paymentdata.com/xsd"; 

XDocument xDoc = XDocument.Load(xml); 
string accountName = xDoc.Root.Element(ns + "return") 
    .Element(ax21 + "Name").Value;