0

I am getting response from web service in xml format and data are inside cData section in xml nodes. now when i am trying to extract node value then getting value with cdata text. how can i remove this and get text inside cdata. Please suggest me the best way of doing it. write now i am using regular expression to remove cdata from the result value. Thanks in advance.![!CDATA[Xyz]].

<ResumeParserData>
    <ResumeFileName><![CDATA[]]></ResumeFileName>
    <ParsingDate><![CDATA[3/29/2012 1:37:33 AM]]></ParsingDate>
    <TitleName><![CDATA[]]></TitleName>
    <FirstName><![CDATA[abc]]></FirstName>
    <Middlename><![CDATA[Kr]]></Middlename>
    <LastName><![CDATA[abc]]></LastName>
    <Email><![CDATA[[email protected]]]></Email>
    <Phone><![CDATA[+91 8527502445]]></Phone>
</ResumeParserData>
2
  • - <ResumeParserData> <ResumeFileName><![CDATA[]]></ResumeFileName> <ParsingDate><![CDATA[3/29/2012 1:37:33 AM]]></ParsingDate> <TitleName><![CDATA[]]></TitleName> <FirstName><![CDATA[abc]]></FirstName> <Middlename><![CDATA[Kr]]></Middlename> <LastName><![CDATA[abc]]></LastName> <Email><![CDATA[[email protected]]]></Email> <Phone><![CDATA[+91 8527502445]]></Phone> - <ResumeParserData> Commented Mar 29, 2012 at 5:39
  • @Anu - can you show us the code you're using for attempting to parse it currently? Showing the xml data is nice, but we can't help you debug the cdata issue if you don't show us what you're trying :) Commented Mar 29, 2012 at 6:42

1 Answer 1

7

right now i am using regular expression to remove cdata from the result value

Well I'd stop using regular expressions to parse XML to start with.

Use an XML API - LINQ to XML is lovely, for example. This should transparently let you get at the text, without you even having to know it was ever in a CDATA section unless you really want to. For example, with an XML file like this:

<parent>
  <child>
    <![CDATA[Foo]]>
  </child>
</parent>

We can get to the text like this:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        var doc = XDocument.Load("test.xml");
        var child = doc.Root.Element("child");
        var text = child.Value;
        Console.WriteLine("Text: {0}", text);
    }
}

EDIT: With a slight change to the code above:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        var doc = XDocument.Load("test.xml");
        foreach (var child in doc.Root.Elements())
        {
            Console.WriteLine("{0}: {1}",
                              child.Name, child.Value);
        }
    }
}

... when I run that on the XML in the question, I get:

ResumeFileName:
ParsingDate: 3/29/2012 1:37:33 AM
TitleName:
FirstName: abc
Middlename: Kr
LastName: abc
Email: [email protected]
Phone: +91 8527502445
Sign up to request clarification or add additional context in comments.

8 Comments

but i still getting cdata in my text.
@Anu: It's hard to know what you're doing wrong as you haven't updated your question with what you're now trying.
@Anu - In case it helps, since you mentioned this is a web response, if you have the string already, you'll use XDocument.Parse instead (Load is for loading from a file, as Jon shows)
@JamesManning: Or a Stream, which would be more suitable for a web response: XDocument.Load(webResponse.GetResponseStream())
code abv working but when i tried this with actual then fails. when i saw my response as text i found &lt; before and after cdata is this happening due to this. <ResumeParserData><ResumeFileName>&lt;![CDATA[]]&gt;</ResumeFileName> <ParsingDate>&lt;![CDATA[3/29/2012 3:33:07 AM]]&gt;</ParsingDate> <TitleName>&lt;![CDATA[]]&gt;</TitleName><ResumeParserData> <FirstName>&lt;![CDATA[xyz]]&gt;</FirstName> <Middlename>&lt;![CDATA[Kr]]&gt;</Middlename> <LastName>&lt;![CDATA[abc]]&gt;</LastName> <Email>&lt;![CDATA[[email protected]]]&gt;</Email> <Phone>&lt;![CDATA[+91 123456789]]&gt;</Phone>
|

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.