0

I have the following XML, in a RestSharp RestRequest.Content object. How can i traverse it, or change to JSON, to get the FULLNAME node out, in C#?

<ns1:MT_Get_Name_Res xmlns:ns1="http://hse.pd.com">
    <fullname>Gandalf Elizabeth Cfieulle02</fullname>
    <error>Success</error>
</ns1:MT_Get_Name_Res>

Things I have tried already:

JSON - returns a screwed up JSON object, which then fails to parse to my local object

XmlDocument doc = new XmlDocument();
doc.LoadXml(response.Content);
string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
EmployeeSAPObject emp = JsonConvert.DeserializeObject<EmployeeSAPObject>(json);

C# XML parsing - fullname is null:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(response.Content); // suppose that myXmlString contains "<Names>...</Names>"

XmlNodeList xnList = xmlDoc.SelectNodes("/MT_Get_Name_Res/fullname");
foreach (XmlNode xn in xnList)
{
    string fullName = xn["fullname"].InnerText;
}

Here is my EmployeeSAPObject class

public class EmployeeSAPObject
{
    public string fullname { get; set; }
    public string error { get; set; }
}
jordan
  • 3,436
  • 11
  • 44
  • 75
  • The sample XML uses an XML namespace, which your XML parsing code doesn't account for; perhaps that's why your `SelectNodes` isn't giving you what you expect. – Jacob Feb 26 '15 at 18:07
  • 1
    And I'd avoid going through a JSON library to deserialize XML; there are XML serialization libraries built into .NET. – Jacob Feb 26 '15 at 18:08
  • Maybe see http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document/19613934#19613934 – Jacob Feb 26 '15 at 18:09

2 Answers2

1

You need to add the namespace and you want to use XDocument rather than XmlDocument:

void Main()
{
    var data = @"<ns1:MT_Get_Name_Res xmlns:ns1=""http://hse.pd.com"">
    <fullname>Gandalf Elizabeth Cfieulle02</fullname>
    <error>Success</error>
</ns1:MT_Get_Name_Res>";

    var xdoc = XDocument.Parse(data);
    Console.WriteLine(xdoc.ToString());

    XNamespace ns = "http://hse.pd.com";
    var result = xdoc.Element(ns + "MT_Get_Name_Res").Element("fullname").Value;
    Console.WriteLine(result);

    Console.ReadLine();
}
Murph
  • 9,985
  • 2
  • 26
  • 41
  • The thing that was causing the problem was the namespace - once you deal with that it all kinds of falls into place. Serialization is also very useful - again it kind of depends on what you want to do. Mostly though, just glad I could help quickly (-: – Murph Feb 28 '15 at 08:43
1

Decorate your EmployeeSAPObject with the appropriate attributes and deserialize it:

[XmlRoot("MT_Get_Name_Res", Namespace = "http://hse.pd.com")]
public class EmployeeSAPObject
{
    [XmlElement(Namespace="")]
    public string fullname { get; set; }
    [XmlElement(Namespace = "")]
    public string error { get; set; }
}

And then use it like:

        EmployeeSAPObject employee;
        using (StringReader reader = new StringReader(response.Content))
            employee = (EmployeeSAPObject)(new XmlSerializer(typeof(EmployeeSAPObject))).Deserialize(reader);

        Debug.Assert(employee.fullname == "Gandalf Elizabeth Cfieulle02"); // No assert
dbc
  • 104,963
  • 20
  • 228
  • 340