ฉันต้องการแยกไฟล์ xml จาก URL ต่อไปนี้:"http://restservice.com/RestServiceImpl.svc/ghhf/cvr"
ฉันสามารถรับ XDocument ได้โดยใช้รหัสต่อไปนี้:
private void Search(object sender, RoutedEventArgs e)
{
string url = "http://restservice.schoolpundit.com/RestServiceImpl.svc/search_name/cvr";
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri(url));
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
XDocument doc = XDocument .Parse(e.Result);
var list = from child in doc.Descendants("Institutions_search_name")
select new listrows {
inst_name=doc.Element("Inst_name").Value;
};
Listbox.ItemSource=list;
}
แต่มันไม่แสดง Inst_name ใดๆ จริงๆ แล้วมันไม่ได้เข้าไปใน doc.Descendants("Institutions_search_name") มันไม่แสดงข้อยกเว้นใดๆ เช่นกัน หากฉันทำผิดพลาด โปรดแก้ไขให้ถูกต้องด้วย
list
ที่ใดก็ได้... แต่ควรจะจบลงด้วยการใช้วิธีdoc.Descendants
แต่คุณไม่ได้ทำอะไรกับผลลัพธ์ คุณได้ตรวจสอบในดีบักเกอร์ว่าการแยกวิเคราะห์สำเร็จหรือไม่ โปรดทราบว่าโค้ดปัจจุบันของคุณจะไม่คอมไพล์เมื่อคุณพยายามใช้คุณสมบัติชื่อvalue
เมื่อคุณสมบัติจริงเรียกว่าValue
ไม่มีประโยชน์ที่จะให้โค้ดของคุณเหมือนกับโค้ดของคุณ คุณควรให้โค้ด จริง แก่เราเพื่อแสดงให้เห็นถึงปัญหา - person Jon Skeet   schedule 08.05.2013XDocument.Parse
ทำงานหรือไม่ (และโค้ดในคำถามของคุณมีรูปแบบที่ น่ากลัว โปรดใช้ความพยายามเล็กน้อยเพื่อทำให้สามารถอ่านได้ โดยมีการเยื้องสม่ำเสมอ และไม่มีช่องว่างในแนวตั้งจำนวนมากโดยไม่มีเหตุผล) - person Jon Skeet   schedule 08.05.2013;
หลังValue
คุณควรจะมีตัวอย่างสั้นๆ แต่ครบถ้วน ซึ่งคุณสามารถโพสต์ คำต่อคำ ก่อนที่จะถามคำถาม - person Jon Skeet   schedule 08.05.2013