0

Hello i am trying to get simple xml file from server and to read the data so i can convert it to list. I was try few lib and code with no success so far. I am getting the xml content in one line without any tags <> and the count is always 0 zero. The XML string. I need to get the data that inside camp tag

<campaigns>
 <mainPage>http://example.com</mainPage>
 <orderPage>https://www.example.co.il/adver/</orderPage>
 <totalCount>3</totalCount>
 <onLineCount>2</onLineCount>
  <campaignList>
   <camp id="557">
    <name>test1</name>
    <status>on</status>
    <rating>5</rating>
    <url>http://example.com/557</url>
  </camp>
  <camp id="559">
   <name>test1</name>
   <status>on</status>
   <rating>5</rating>
   <url>http://example.com/559</url>
 </camp>
 <camp id="660">
  <name>test1</name>
  <status>off</status>
  <rating>5</rating>
  <url>http://example.com/660</url>
 </camp>
</campaignList>

And the c# code i am trying so far

XElement xelement = XElement.Load("http://example.com/test.xml");

    var name = from nm in xelement.Elements("camp")
               where (string)nm.Element("status") == "on"
               select nm;

    Response.Write(name.Count());

    foreach (XElement xEle in name)
    {
        Response.Write(xEle);
    }
Elidotnet
  • 291
  • 3
  • 18
  • It would be easier to read the XML if you'd indent it appropriately - at a glance, it looks like `campaignList` is a child element of `onLineCount`. – Jon Skeet Dec 05 '17 at 08:23

1 Answers1

0

XElement.Elements() means search in children tags. I think what you need is Descendants() or xelement.Element("campaigns").Element("campaignList").Elements("camp")

ajshort
  • 3,684
  • 5
  • 29
  • 43
Simon
  • 175
  • 1
  • 4
  • Thanks for reply, I am getting Object reference not set to an instance of an object message @Simon – Elidotnet Dec 05 '17 at 05:43
  • I looks like you are not getting any data. Probably you need to use credentials or a proxy to get the data.So using a webclient instead of using XElement Load would be the solution.Before jumping to any conclusions I recommend trying two things 1) Put url into a IE and see the results 2) Use a sniffer to get actual error.Net is not very good at getting the low level errors from a connectionHow did you get the xml file posted? I would use sniffer results of getting good xml and compare with you application and make app look like good results.Usually with webclient you need to add html headers – jdweng Dec 05 '17 at 07:22
  • @jdweng i was changed the url for privacy reason. the original url is working well and the data is existing – Elidotnet Dec 05 '17 at 07:51
  • Just get rid of `Element("campaigns")` here - the root element *is* `campaigns` (and the OP is loading as an `XElement` rather than an `XDocument`). – Jon Skeet Dec 05 '17 at 08:24