Parsing complex XML with C#

后端 未结 2 572
渐次进展
渐次进展 2021-01-20 00:52

I am trying to parse a complex XML with C#, I am using Linq to do it. Basically, I am doing a request to a server and I get XML, this is the code:

XElement x         


        
2条回答
  •  孤街浪徒
    2021-01-20 01:44

    Currently your XML is invalid since the test namespace is not declared, you can declare it like this:

    
      1282570
      SLAYERTANIC
      aaa
      aaa
    
    

    Having this you can use XNamespace to qualify the XML element you want with the correct namespace:

    XElement xdoc = XElement.Parse(e.Result);
    XNamespace test = "http://foo.bar";
    this.newsList.ItemsSource = from item in xdoc.Descendants("item")
                                select new ArticlesItem
                                {
                                    LinkID = item.Element(test + "link_id").Value,
                                    Title = item.Element("title").Value,
                                    Description = this.Strip(item.Element("description").Value).Substring(0, 200).ToString()
                                }
    

提交回复
热议问题