I'm getting a string returned from a website that looks like this
<?xml version=\"1.0\" encoding=\"UTF-8\"?><searchResponse requestID=\"500\" status=\"success\"><pso><psoID ID=\"61F2C644-F93A-11DE-8015-73A11AB14291\" targetID=\"mezeoAccount\"><data><email>sholobfc@bluefire.com.au</email><quotaMeg>2048</quotaMeg><quotaUsed>1879736</quotaUsed><active>true</active><unlocked>true</unlocked><allowPublic>true</allowPublic><realm>mezeo</realm><bandwidthQuota>1000000000</bandwidthQuota><billingDay>1</billingDay></data></psoID></pso></searchResponse>"
I then try and create an XDocument from it so I can enumerate through the elements
XDocument doc = new XDocument();
doc = XDocument.Parse(respStr);
but if I query the elements or descendants everytime it returns null. I can't go
string s = doc.Element("email").Value;
// or
doc.Descendants("data"); // returns null as well
XDocument.Parse doesn't return an error, but I don't seem to have a searchable xDocument.
Can anyone see anything obviously wrong with what I am doing?
You don't need to create a new XDocument before calling XDocument.Parse. This won't cause any problems, it's just pointless.
However, this line is wrong because email is not a child of the document root:
doc.Element("email").Value;
Your second example looks fine. This works for me:
string s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><searchResponse requestID=\"500\" status=\"success\"><pso><psoID ID=\"61F2C644-F93A-11DE-8015-73A11AB14291\" targetID=\"mezeoAccount\"><data><email>sholobfc@bluefire.com.au</email><quotaMeg>2048</quotaMeg><quotaUsed>1879736</quotaUsed><active>true</active><unlocked>true</unlocked><allowPublic>true</allowPublic><realm>mezeo</realm><bandwidthQuota>1000000000</bandwidthQuota><billingDay>1</billingDay></data></psoID></pso></searchResponse>";
XDocument doc = XDocument.Parse(s);
foreach (XElement e in doc.Descendants("data"))
Console.WriteLine(e);
Result:
<data>
<email>sholobfc@bluefire.com.au</email>
<quotaMeg>2048</quotaMeg>
<quotaUsed>1879736</quotaUsed>
<active>true</active>
<unlocked>true</unlocked>
<allowPublic>true</allowPublic>
<realm>mezeo</realm>
<bandwidthQuota>1000000000</bandwidthQuota>
<billingDay>1</billingDay>
</data>
In response to your second third question (see comments to this answer) try this:
using System;
using System.Xml.XPath;
using System.Xml.Linq;
class Program
{
public static void Main()
{
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><searchResponse requestID=\"500\" status=\"success\"><pso><psoID ID=\"61F2C644-F93A-11DE-8015-73A11AB14291\" targetID=\"mezeoAccount\"><data><email>sholobfc@bluefire.com.au</email><quotaMeg>2048</quotaMeg><quotaUsed>1879736</quotaUsed><active>true</active><unlocked>true</unlocked><allowPublic>true</allowPublic><realm>mezeo</realm><bandwidthQuota>1000000000</bandwidthQuota><billingDay>1</billingDay></data></psoID></pso></searchResponse>";
XDocument doc = XDocument.Parse(xml);
foreach (XElement e in doc.XPathSelectElements("/searchResponse/pso/psoID/data/*"))
Console.WriteLine(e);
}
}
Output:
<email>sholobfc@bluefire.com.au</email>
<quotaMeg>2048</quotaMeg>
<quotaUsed>1879736</quotaUsed>
<active>true</active>
<unlocked>true</unlocked>
<allowPublic>true</allowPublic>
<realm>mezeo</realm>
<bandwidthQuota>1000000000</bandwidthQuota>
<billingDay>1</billingDay>