this way i am reading a xml file with linq and getting error Invalid character in the given encoding
XDocument document = XDocument.Load(@"c:\users\tridip\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Orders.xml");
var books = from r in document.Descendants("book")
select new
{
OrderID = r.Element("OrderID").Value,
CustomerID = r.Element("CustomerID").Value,
EmployeeID = r.Element("EmployeeID").Value,
};
i have see this kind of data in xml
<Orders>
<OrderID>10249</OrderID>
<CustomerID>TOMSP</CustomerID>
<EmployeeID>6</EmployeeID>
<OrderDate>1996-07-05T00:00:00</OrderDate>
<RequiredDate>1996-08-16T00:00:00</RequiredDate>
<ShippedDate>1996-07-10T00:00:00</ShippedDate>
<ShipVia>1</ShipVia>
<Freight>11.6100</Freight>
<ShipName>Toms Spezialitäten</ShipName>
<ShipAddress>Luisenstr. 48</ShipAddress>
<ShipCity>Münster</ShipCity>
<ShipPostalCode>44087</ShipPostalCode>
<ShipCountry>Germany</ShipCountry>
</Orders>
see this <ShipName>Toms Spezialitäten</ShipName>
there are some invalid or unknown character in text. how to get rid of this. i want to read xml if there exist unknown character in xml data. please guide me what to add in my above code to handle this situation. thanks
issue solved
the moment i add encoding in my xml data like this way the it started working
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Orders>
<OrderID>10248</OrderID>
<CustomerID>VINET</CustomerID>
<EmployeeID>5</EmployeeID>
<OrderDate>1996-07-04T00:00:00</OrderDate>
<RequiredDate>1996-08-01T00:00:00</RequiredDate>
<ShippedDate>1996-07-16T00:00:00</ShippedDate>
<ShipVia>3</ShipVia>
<Freight>32.3800</Freight>
<ShipName>Vins et alcools Chevalier</ShipName>
<ShipAddress>59 rue de l'Abbaye</ShipAddress>
<ShipCity>Reims</ShipCity>
<ShipPostalCode>51100</ShipPostalCode>
<ShipCountry>France</ShipCountry>
</Orders>
<Orders>
.... more data
</Orders>
</Root>
private void button1_Click(object sender, EventArgs e)
{
XDocument document = XDocument.Load(@"c:\users\tridip\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Orders.xml");
var books = from r in document.Descendants("Orders")
select new
{
OrderID = r.Element("OrderID").Value,
CustomerID = r.Element("CustomerID").Value,
EmployeeID = r.Element("EmployeeID").Value,
};
dataGridView1.DataSource= books.ToList();
}