2

I have following xml in string variable-

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
  <first-name>RaJeEv(๏๏)</first-name>
  <last-name>Diboliya</last-name>
  <headline>Software Engineer at FASTTRACK INDIA.</headline>
  <site-standard-profile-request>
    <url>http://www.linkedin.com/profile?viewProfile=&amp;url>
  </site-standard-profile-request>
</person>

Now I want to get first and last name from this string. How can I do this?

7
  • What have you already tried, what approaches have you considered? Commented Jan 24, 2013 at 8:34
  • 1
    I have not tried anything, because I dont have any idea about this. Commented Jan 24, 2013 at 8:34
  • You can also create a strongly typed class and deserialize the data into it. Commented Jan 24, 2013 at 8:35
  • @Middas: How can I deserialize the class? Commented Jan 24, 2013 at 8:36
  • Create a class that matches the data then use the XmlSerializer to deserialize your XML data into that class. Here is an example site: jonasjohn.de/snippets/csharp/xmlserializer-example.htm Commented Jan 24, 2013 at 8:39

5 Answers 5

2

for example

public class Program {
    public static void Main(String[] args) {
        XDocument xdoc = XDocument.Parse(@"<?xml version=""1.0"" encoding=""UTF-8""     standalone=""yes""?>
<person>
  <first-name>RaJeEv(๏๏)</first-name>
  <last-name>Diboliya</last-name>
  <headline>Software Engineer at FASTTRACK INDIA.</headline>
  <site-standard-profile-request>
    <url>http://www.linkedin.com/profile?viewProfile</url>
  </site-standard-profile-request>
</person>");

        XElement xe = xdoc.Elements("person").First();

        Console.WriteLine("{0} {1}", xe.Element("first-name").Value, xe.Element("last-name").Value);
    }         
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here's how I would deserialize this -

Create a concrete domain class Person

[Serializable()]
public class Person
{
    [System.Xml.Serialization.XmlElementAttribute("first-name")]
    public string FirstName{ get; set; }

    [System.Xml.Serialization.XmlElementAttribute("last-name")]
    public string LastName{ get; set; }

    [System.Xml.Serialization.XmlElementAttribute("headline")]
    public string Headline{ get; set; }

    [System.Xml.Serialization.XmlElementAttribute("site-standard-profile-request")]
    public string ProfileRequest{ get; set; }
}

The use XmlSerializer to convert it to Person type

XmlSerializer serializer = new XmlSerializer(typeof(Person));
var person = serializer.Deserialize(xml) as Person;

The properties can then be accessed like

var firstName = person.FirstName;
var lastName = person.LastName;

Comments

0

Right on the MSDN

Parse XML with XmlReader

but if you have this struct in a class strong type you can also see this answer on how you convert it to xml and back: Send XML String as Response

Comments

0
var person = XElement.Parse(yourString).Element("person");
string firstName = person.Element("first-name").Value;
string lastName = person.Element("last-name").Value;

1 Comment

Could you add some comments to your code? It will be more meaningful for others. Check this metaSO question and Jon Skeet: Coding Blog on how to give a correct answer.
0

This is what you are looking for..

        XmlDocument xmldoc = new XmlDocument();
        XmlNodeList xmlnode;
        FileStream fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read);
        xmldoc.Load(fs);

        xmlnode = xmldoc.GetElementsByTagName("first-name");
        string firstname= string.Empty;
        if(xmlnode!=null)
            strOption = Regex.Replace(xmlnode[0].InnerText, @"\t|\n|\r| ", "");

        xmlnode = xmldoc.GetElementsByTagName("last-name");
        string lastname= string.Empty;
        if(xmlnode!=null)
            strOption = Regex.Replace(xmlnode[0].InnerText, @"\t|\n|\r| ", "");

Hope it helps :)

3 Comments

no offense meant because ok, this works, but there's no added value in telling someone, who's trying to understand how to extract values from an XML string, to use Regex. That's over-complicating things. C# has methods built in to deal with XPath and to get to the InnerText of an XML node.
@WimOmbelets : That's not over-complicating, that's necessary, because if your xml has values like tab/new-line for viewing-purpose, then you gonna have to replace each tab,new-line,trim to get the absolute string. And Regex is way more faster to do this that individual string.Replace and Trim.
if XML has 'presentation' characters in its values, you track down who did that and [insert very bad juju here]

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.