1

I have an XML that is not well formatted but needs to be consumed:

<Users>
<User First="aaa" Second="bbb">InnerValue</User>
<User First="bbb" Second="">InnerValue</User>
</Users>

Clases definitions:

public class Users
    {            
        public List<User> User{ get; set; }
    }

 public class User
    {        
        [JsonProperty("@First")]
        public string First{ get; set; }

        [JsonProperty("@Second")]
        public string Second{ get; set; }

        //how to define a property to get the InnerValue
    }

To parse:

XDocument xmlDocument = XDocument.Parse(xmlData);
string jsonData = JsonConvert.SerializeXNode(xmlDocument);
Users users = JsonConvert.DeserializeObject<Users>(jsonData);

So everything is nicely deserialized but how to get the inner value?

5
  • I don't understand, what exactly is the problem? Commented Feb 15, 2013 at 16:21
  • I can't get the InnerValue as i have explained. Commented Feb 15, 2013 at 16:22
  • hey @plurby any special reason to use Json.net ? Commented Feb 15, 2013 at 16:34
  • @BaljeetsinghSucharia It's a requirement. Commented Feb 15, 2013 at 16:37
  • hmm ok got - but if not json i hope you know that this can also be useful var usersList = from s in xml.Descendants("Users") select s; - loop through each userItem - each userItem can give you value Commented Feb 15, 2013 at 16:41

1 Answer 1

1

To get the inner value you must use [JsonProperty("#text")] so to update:

public class User
    {        
        [JsonProperty("@First")]
        public string First{ get; set; }

        [JsonProperty("@Second")]
        public string Second{ get; set; }

        [JsonProperty("#text")]
        public string InnerValue { get; set; }
    }
Sign up to request clarification or add additional context in comments.

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.