You can use XDocument to deal with the problem by looping and fetching the type's value as well as to tell whether the type belongs to "text/html":
XDocument doc = XDocument.Load("XMLFile1.xml");
var result = from e in doc.Descendants("body")
select new
{
Context = e.Element("content").Value,
Type = e.Element("type").Value.Equals("text/html") ? string.Empty : e.Element("type").Value
};
sahil158
Member
24 Points
31 Posts
Please help to parse
Apr 25, 2012 04:30 PM|LINK
i want to fetch the content of body node based on type node of body node.
if the type is text/html then only i want the content data..
please help...
parsing
ramanselva
Contributor
2064 Points
324 Posts
Re: Please help to parse
Apr 26, 2012 05:41 AM|LINK
Hi,
you can parse as given,
string content=@"<data> <body> <content></content> <type>text/plain</type> </body> <body> <content></content> <type>text/html</type> </body> </data>"; XmlDocument xml = new XmlDocument(); xml.LoadXml(content); XmlNodeList _list = xml.SelectNodes("//data/body[type='text/html']"); foreach (XmlNode _item in _list) { string data = _item.FirstChild.InnerText; string _type = _item.LastChild.InnerText; }This can be beneficial to other community members reading the thread.
Regards,
Rama Selvam M.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Please help to parse
Apr 27, 2012 02:00 AM|LINK
Hello sahil158:)
You can use XDocument to deal with the problem by looping and fetching the type's value as well as to tell whether the type belongs to "text/html":
XDocument doc = XDocument.Load("XMLFile1.xml"); var result = from e in doc.Descendants("body") select new { Context = e.Element("content").Value, Type = e.Element("type").Value.Equals("text/html") ? string.Empty : e.Element("type").Value };parsing
kavita_khand...
Star
9767 Points
1930 Posts
Re: Please help to parse
Apr 27, 2012 08:48 AM|LINK
string myXml = "<data>" + "<body>" + "<content>" + "</content>" + "<type>text/plain</type>" + "</body>" + "<body>" + "<content>" + "</content>" + "<type>text/html</type>" + "</body>" + "</data>"; XElement xe = XElement.Parse(myXml); IEnumerable<XElement> textHtmlData = from s in xe.Elements("body").Elements("type") where s.Value.Equals("text/html") select s;parsing
I would love to change the world, but they wont give me the source code.