0

I am trying to read Message value from below xml string. How read it?

        <Messages> 
           <Exceptions />  
               <ValidationIssues>
                   <ValidationMessage Message="The Customer Communication requires a value for Search Phone or Email." FriendlyMessage="\" />  
                </ValidationIssues>
         </Messages>"   

tried

 var values = (from element in doc.XPathSelectElements("/Messages/Validationsissues/validationmessage")
                   where element.Attribute("message") != null
                   select (element.Attribute("message").Value));

did

5
  • Is XPathSelectElements case sensitive? Commented Apr 5, 2015 at 23:49
  • Have you fixed your capitalization? Also, your intention is misleading, shouldn't intent extra after a self closing tag. Commented Apr 5, 2015 at 23:50
  • Also, 'ValidationIssues' is spelled different to 'Validationsissues'; there's an extra 's' in the middle. Commented Apr 5, 2015 at 23:50
  • 1
    Where specifically does this fail? Does doc.XPathSelectElements("/Messages/Validationsissues/validationmessage") find what you expect? Do the matched elements have a message attribute? Don't just randomly write code and give up when it doesn't work. Debug it. Commented Apr 5, 2015 at 23:51
  • @James123 Could you respond to the questions in the comments and answers on your question. If you have found a solution yourself, please post this as the answer. Commented Apr 13, 2015 at 12:18

4 Answers 4

1

XPathSelectElements is case-sensitive. Change element.Attribute("message") to element.Attribute("Message").

Sign up to request clarification or add additional context in comments.

Comments

0

Here's how:

XmlDocument doc = new XmlDocument();
doc.LoadXML("<Messages> 
           <Exceptions />  
               <ValidationIssues>
                   <ValidationMessage Message=\"The Customer Communication requires a value for Search Phone or Email.\" FriendlyMessage=\"\" />  
                </ValidationIssues>
         </Messages>"  );


String str = doc.SelectSingleNode("//Messages/ValidationIssues/ValidationMessage").Attributes["Message"].Value;

You can also see my other answer at: How to use XMLNode.SelectSingleNode

Comments

0

The easiest thing would be to do this using an XDocument and no XPath but simply selecting the "ValidationIssues" elements as in:

class Program
{
    static string my_xml = 
        "<Messages> " +
        "    <Exceptions />" +
        "    <ValidationIssues>" +
        "        <ValidationMessage Message=\"The Customer Communication requires a value for Search Phone or Email.\" FriendlyMessage=\"\\\" />  " +
        "    </ValidationIssues>" +
        "</Messages>";

    public static void Main(params string[] args)
    {
        var doc = XDocument.Parse(my_xml, LoadOptions.PreserveWhitespace);
        var messages = doc
            .Descendants("ValidationMessage")
            .Where(x => x.Attribute("Message") != null)
            .Select(x => x.Attribute("Message").Value);
        Console.WriteLine(string.Join(Environment.NewLine, messages));
        Console.ReadLine();
    }
}

Comments

0

Note that XML is case sensitive. XML elements and attributes name need to be mentioned in your XPath using the exact same cases.

If you like XPath, you can also add Message attribute checking in the XPath. For example, this XPath only return ValidationMessage having XML attribute Message :

/Messages/ValidationIssues/ValidationMessage[@Message]

Another tips, you may prefer casting XElement or XAttribute to string -or to some other possible types- instead of accessing Value property. This approach has advantages, for example, you can avoid null reference exception in case the attribute/element not found (never happen in this case because we already validate the attribute existence in the from/where clause), besides you can possibly get the attribute/element value readily in the suitable data type :

var xpath = "/Messages/ValidationIssues/ValidationMessage[@Message]";
var values = (from element in doc.XPathSelectElements(xpath)
              select (string)element.Attribute("message"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.