1

I want to read two elements which are siblings, please suggest me appropriate way to do it using XmlReader in C#

Here is my XML

    <loginReturn>
        <canumber>100556369</canumber>
        <emailid>[email protected]</emailid>
        <firstname>abc</firstname>
        <lastname>abc</lastname>
        <masterca/>
        <message>Login succesful !</message>
        <meternumber>8683169</meternumber>
        <mobilenumber>1111111111</mobilenumber>
        <status>1</status>
        <subca/>
        <username>abcbbb</username>
     </loginReturn>

I want to read "Status" first and if its not equal to "1" then read "message"...

Here is my code:

        using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
        {
            string msg = Strings.SUCCESS;

            reader.ReadToFollowing("status");
            var x = reader.ReadElementContentAsInt();

            if (x != 1)
            {
                reader.ReadToFollowing("message");
                msg = reader.ReadElementContentAsString();
            }

            return msg;
        }
5
  • I think it's better to read it all at first and on the next step to do some logic work and process your data. Commented Nov 12, 2015 at 9:38
  • Do not assume ordering (even if it seems fixed). Just read both message and status (using ReadToNextSibling() to skip what you don't need) and when you have them then perform your logic (yes it'll subtly hit performance but unless you have to process 10M nodes IMO you won't notice any difference). Commented Nov 12, 2015 at 9:40
  • with 'reader.ReadToFollowing("status")' cursor moves to "status" node.. after that reader only points inside "status" element.. how can i read both elements and store in var.. please suggest with code Commented Nov 12, 2015 at 9:43
  • If my answer could help you to solve your problem, it would be very kind of you to vote it up and mark it as accepted, thx! Commented Nov 12, 2015 at 15:08
  • This seems still to be an open problem... Do you need further help? Commented Nov 27, 2015 at 9:14

1 Answer 1

1

EDIT: Your comment made clear, that XmlDocument is not available in Xamarin. I have no experienc with Xamarin and no chance to test it. But a short research brought me to XPathDocument (to create an XPathNavigator) which seems to be supported in Xamarin. The solution could be something like this:

    string msg="OK";
    if(YourNavigator.Evaluate("/loginReturn/status/text()")!="1")
        msg=YourNavigator.Evaluate("loginReturn/message/text()");

This is completely untested "air-code"...Hope this helps...

Here's my former answer with XmlDocument - regrettfully not working in Xamarin:

    string xml="<loginReturn><canumber>100556369</canumber><emailid>[email protected]</emailid>" +
               "<firstname>abc</firstname><lastname>abc</lastname><masterca/><message>Login succesful !</message>" + 
               "<meternumber>8683169</meternumber><mobilenumber>1111111111</mobilenumber><status>1</status><subca/>" + 
               "<username>abcbbb</username></loginReturn>";
    XmlDocument xdoc= new XmlDocument();
    xdoc.LoadXml(xml);

    string msg="OK";
    if(xdoc["loginReturn"]["status"].InnerText!="1")
        msg=xdoc["loginReturn"]["message"].InnerText;
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is correct, but i cannot use XmlDocument.... i am coding in Xamarin PCL which does not support XmlDocument

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.