Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
XML
<?xml version="1.0" encoding="utf-8" ?>
<ServiceResponse xmlns:xsd="http://www.w3.org/"
xmlns:xsi="http://www.w3.org/"
xmlns="http://www.dealerlink.us/"><Response>REJECTED</Response>
<Message>ZIP OUT OF AREA</Message>
</ServiceResponse>


I have XML as above, I want value "ZIP OUT OF AREA" in string "msg"

For that I wrote a code as below:
C#
XDocument XDOC = XDocument.Parse(xmlstring);
string msg=XDOC.Element("ServiceResponse").Element("Message").Value;

but string returns null.

Please help, I am stuck at this point.
thanks.
Posted
Updated 20-Feb-11 23:05pm
v3

You're running into problems because you're trying to read elements in a namespace without specifying that namespace.

The last attribute of the XML, xmlns="http://www.dealerlink.us" defines the default namespace of the document as http://www.dealerlink.us.

You can fix that by prefixing your element names with the namespace like this:
C#
string xmlstring = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><ServiceResponse xmlns:xsd=\"http://www.w3.org/\" xmlns:xsi=\"http://www.w3.org/\" xmlns=\"http://www.dealerlink.us/\"><Response>REJECTED</Response><Message>ZIP OUT OF AREA</Message></ServiceResponse>";

XNamespace ns = "http://www.dealerlink.us/";
XDocument XDOC = XDocument.Parse(xmlstring);

string msg = XDOC.Element(ns + "ServiceResponse").Element(ns + "Message").Value;


Hope this helps,
Fredrik Bornander
 
Share this answer
 
Comments
Albin Abel 15-Feb-11 9:30am    
my 5
Sandeep Mewara 15-Feb-11 9:32am    
Good answer. 5!
Punit Belani 15-Feb-11 9:38am    
thanks its done now.. :)
Punit Belani 13-Mar-11 14:56pm    
////////////test


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime sDate = Convert.ToDateTime(TextBox1.Text);
DateTime eDate = Convert.ToDateTime(TextBox2.Text);
TimeSpan tSpan = eDate - sDate;
int days = Convert.ToInt32(tSpan.TotalDays);
DataTable dt = new DataTable("Dates");
dt.Columns.Add("DatesCol", typeof(System.DateTime));
DataRow dr;
int pm = sDate.Month;
int py = sDate.Year;
int nth = 0;
for (int i = 0; i <= days; i++)
{
int month = sDate.Month;
int year = sDate.Year;
if (month == pm && year == py)
{
if (sDate.DayOfWeek.ToString() == DropDownList1.SelectedValue)
{
nth = nth + 1;
}
}
else
{
nth = 0;
}
if (sDate.DayOfWeek.ToString() == DropDownList1.SelectedValue && nth == Convert.ToInt32(DropDownList2.SelectedValue))
{
dr = dt.NewRow();
dr["DatesCol"] = sDate;
dt.Rows.Add(dr);
}
sDate = sDate.AddDays(1);
pm = month;
py = year;
}
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Write(GridView1.Rows.Count.ToString());
}
}
Punit Belani 13-Mar-11 14:57pm    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime sDate = Convert.ToDateTime(TextBox1.Text);
DateTime eDate = Convert.ToDateTime(TextBox2.Text);
TimeSpan tSpan = eDate - sDate;
int days = Convert.ToInt32(tSpan.TotalDays);
DataTable dt = new DataTable("Dates");
dt.Columns.Add("DatesCol", typeof(System.DateTime));
DataRow dr;
int pm = sDate.Month;
int py = sDate.Year;
int nth = 0;
for (int i = 0; i <= days; i++)
{
int month = sDate.Month;
int year = sDate.Year;
if (month == pm && year == py)
{
if (sDate.DayOfWeek.ToString() == DropDownList1.SelectedValue)
{
nth = nth + 1;
}
}
else
{
nth = 0;
}
if (sDate.DayOfWeek.ToString() == DropDownList1.SelectedValue && nth == Convert.ToInt32(DropDownList2.SelectedValue))
{
dr = dt.NewRow();
dr["DatesCol"] = sDate;
dt.Rows.Add(dr);
}
sDate = sDate.AddDays(1);
pm = month;
py = year;
}
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Write(GridView1.Rows.Count.ToString());
}
}
//////////////////////test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime sDate = Convert.ToDateTime(TextBox1.Text);
DateTime eDate = Convert.ToDateTime(TextBox2.Text);
TimeSpan tSpan = eDate - sDate;
int days = Convert.ToInt32(tSpan.TotalDays);
DataTable dt = new DataTable("Dates");
dt.Columns.Add("DatesCol", typeof(System.DateTime));
DataRow dr;
int pm = sDate.Month;
int py = sDate.Year;
int nth = 0;
for (int i = 0; i &lt;= days; i++)
{
int month = sDate.Month;
int year = sDate.Year;
if (month == pm &amp;&amp; year == py)
{
if (sDate.DayOfWeek.ToString() == DropDownList1.SelectedValue)
{
nth = nth + 1;
}
}
else
{
nth = 0;
}
if (sDate.DayOfWeek.ToString() == DropDownList1.SelectedValue &amp;&amp; nth == Convert.ToInt32(DropDownList2.SelectedValue))
{
dr = dt.NewRow();
dr["DatesCol"] = sDate;
dt.Rows.Add(dr);
}
sDate = sDate.AddDays(1);
pm = month;
py = year;
}
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Write(GridView1.Rows.Count.ToString());
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900