0
        <?xml version="1.0" encoding="utf-8"?>
         <root>
           <Message type="sms">
           <Details locale="en" message-type="User.ResetPassword" />
           <Context>
           <Parameter name="Time" value=" 16:03:31" />
           <Parameter name="pswr" value="00" />
           <Parameter name="Date" value="18/12/2014" />
          </Context>
         <Receiver>+923328749199</Receiver>
        </Message>

and my code is

     XElement xelement = XElement.Load("C:\\Users\\qadeer.hussain\\Desktop\\gw-msg-2.xml");
    var name = from nm in xelement.Elements("Message")
               where (string)nm.Element("Receiver") == "+923328749199"
               select nm;
    foreach (XElement xEle in name)
        Console.WriteLine(xEle.value);

i dont know why i am getting this error can u please help me to solve this probem

Qadeer Hussain
  • 653
  • 2
  • 7
  • 17

1 Answers1

0

This is a solution that uses Xlement to parse the xml provided, note within the method GetMessageFromXml, the checks for null , the use of FirstOrDefault and the use of Attributes`Elements` to pick apart or traverse the xml.

var xml = XElement.Parse(GetXmlToProcess());
var messages = xml.Elements("Message").Select(x=> 
                      {
                          return GetMessageFromXml(x);
                      });

//method to parse
public Message GetMessageFromXml( XElement elem)
{
        var msg  = new Message();
        var type = elem.Attributes("type").FirstOrDefault();
        msg.Type =type != null? type.Value : null;
        var detail =elem.Elements("Details");
        if (detail != null)
        {
            var locale = detail.Attributes("locale").FirstOrDefault();
            var messageType = detail.Attributes("message-type").FirstOrDefault();
            msg.Details = new Detail()
                            {
                                Locale=locale != null? locale.Value: null,
                                MessageType= messageType != null? messageType.Value: null
                            };
        }

        var reciever =elem.Elements("Receiver").FirstOrDefault();
        if (reciever != null)
        {
            msg.Receiver = reciever.Value;
        }
        var context = elem.Elements("Context");
        if (context != null)
        {
            msg.Context = new Context();
            msg.Context.Parameters.AddRange(context.Elements("Parameter").Select(x=>
            {
                var parameter = new Parameter();
                var name = x.Attributes("name").FirstOrDefault();
                var value = x.Attributes("value").FirstOrDefault();
                parameter.Name = name!= null? name.Value:null;
                parameter.Value = value!=null? value.Value:null;
                return parameter;
            }).ToList());
        }
        return msg;
}

the classes used

public class Message
{
    public string Type{get; set;}

    public Detail Details {get; set;}

    public Context Context {get; set;}

    public string Receiver{get;set;}
}

public class Detail
{
    public string Locale{get;set;}
    public string MessageType{get;set;}
}

public class Context
{
    public List<Parameter> Parameters {get;set;}

    public Context()
    {
        Parameters =  new List<Parameter>();
    }
}   

public class Parameter
{   
    public string Name {get;set;}

    public string Value {get;set;}
}
TYY
  • 2,702
  • 1
  • 13
  • 14
  • As an aside, if the OP really wanted to deserialize the whole she-bang into an object model then `XmlSerializer` is far better suited than hand writing all this code. – Charles Mager May 19 '15 at 11:40
  • You really should explain why this answers the question. Code-only answers aren't very instructive. Often they're automatically marked as "low quality" and deleted in due time. – Gert Arnold May 19 '15 at 11:43
  • Well The OP is having problems parsing xml. What I have given him, is a working solution so he actually can see something that works for his problem set. While we can send to go figure out how to fix a null pointer, the root issue is that he is trying to parse his xml and doesn't seem to know how. – TYY May 19 '15 at 11:44
  • @Charles, I can't speculate what the OP is trying to do past parsing that piece of xml, I am just giving him a way to understand how linq helps him parse xml. – TYY May 19 '15 at 11:51
  • @Gert there is a reason I did not explain the code, he has asked the same question twice. And knowing that he is having problem understanding how to parse xml, I have given him a simple enough solution that allows him to walk through the code (but forcing him to explore as well). Note this is why I went through the steps of creating classes and not using anonymous types. – TYY May 19 '15 at 11:51
  • Whatever. I'm just warning you about this low-quality queue which has a way of erasing code-only answers. – Gert Arnold May 19 '15 at 11:56