XML VS JSON第一
,如果你要使用API來執行一些AJAX查詢(比如,查詢API當用戶點擊一個鏈接/圖像,例如,你想改變該鏈接的顏色,女巫會告訴用戶它是好的或不是...去JSON,因爲你不需要解析XML)
如果你在做任何事情「灌木叢」,你只需要呈現在後面的代碼中處理的數據,然後使用XML。
簡單使用,在這個時候WebClient的對象
private string GetDocument(string myPin) {
String url = String.Format("http://someservice.com/name_query.php?pincode={0}", myPin);
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;)"); // pass as Internet Explorer 7.0
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
}
,你有你在一個字符串的API得到了完整的XML,你現在需要的是處理XML,例如,如:
想象的輸出是等的XML文檔:
<Hit dbId="400179221" systemId="115">
<WorksiteDbId>200105072</WorksiteDbId>
<Subscribed>false</Subscribed>
<FirstName>Klaus Holse</FirstName>
<LastName>Andersen</LastName>
<Status>Active</Status>
<MainJobTitle>CEO (Managing Director, General Manager, Owner)</MainJobTitle>
<WorksiteName>Microsoft Development Center Copenhagen ApS </WorksiteName>
<Department></Department>
<Address></Address>
<Zipcode></Zipcode>
<City></City>
<WorksitePhone></WorksitePhone>
<TypeCode>TY10</TypeCode>
<WorksiteStatus>Active</WorksiteStatus>
</Hit>
的方法來處理文檔信息是這樣的:
private void processDocument(string myPin) {
String xml = GetDocument(myPin);
XmlTextReader reader = new XmlTextReader(new StringReader(xml));
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNodeList list = document.SelectNodes("/XMLNode/XMLSubNode");
foreach (XmlNode node in list) // loop through all nodes
{
foreach (XmlAttribute att in node.Attributes) // loop through all attributes
{
switch (att.Name.ToLower())
{
case "dbid": myClass.DbID = Int32.Parse(att.InnerText); break;
case "systemid": myClass.SystemID = Int32.Parse(att.InnerText); break;
default: break;
}
}
foreach (XmlNode subnode in node.ChildNodes) // loop through all subnodes
{
switch (subnode.Name.ToLower()) // check what node has what
{
case "subscribed": myClass.Subscribed = bool.Parse(subnode.InnerText); break;
case "firstname": myClass.Firstname = subnode.InnerText; break;
case "lastname": myClass.Lastname = subnode.InnerText; break;
case "status": myClass.Status = subnode.InnerText; break;
...
}
}
}
}
,你將有myClass的充滿了由API所返回的所有值...
爲您在第一線提...這是新手:)和這是一個很好的方式來了解獲取和使用XML數據的概念...在瞭解了這部分之後,然後您將輕鬆移動到LINQ2XML :)
我希望這有助於...
添加
,因爲我現在纔看到你的XML的輸出,這裏是使用精確的XML
XML的processDocument方法:
<?xml version="1.0" encoding="UTF-8"?>
<Address>
<Country>US</Country>
<City>Seattle</City>
<Result>Done</Result>
</Address>
方法:
private void processDocument(string myPin) {
String xml = GetDocument(myPin);
XmlTextReader reader = new XmlTextReader(new StringReader(xml));
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNodeList list = document.SelectNodes("/Address");
foreach (XmlNode node in list) // loop through all nodes
{
foreach (XmlNode subnode in node.ChildNodes) // loop through all subnodes
{
switch (subnode.Name.ToLower()) // check what node has what
{
case "country": myClass.Country =subnode.InnerText; break;
case "city": myClass.City= subnode.InnerText; break;
case "result": myClass.Result = subnode.InnerText; break;
}
}
}
}
記得檢查錯誤,比如傳遞一組錯誤的數據,以便正確處理錯誤。
:-)
謝謝david..how我可以從返回XML或JSON的url中獲取xmlSourceString嗎? – Musa 2009-05-26 12:15:20
您可以將變量xmlSourceString設置爲從打開URL並按照balexandre描述的方式使用WebClient讀取流時返回的XML。 – 2009-05-26 12:22:17