0

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();
        }
Mou
  • 15,673
  • 43
  • 156
  • 275
  • I think I asked you this before, but try to search first, try to include what you have researched in your question and start your sentences with a Capital. Anyway see duplicate or [any](http://stackoverflow.com/questions/8275825/) [of](http://stackoverflow.com/questions/20939946/) [the](http://stackoverflow.com/questions/14281408/) [many](http://stackoverflow.com/questions/6829715/) [other](http://stackoverflow.com/questions/10455335/) [questions](http://stackoverflow.com/questions/854335/) about this very issue. It's got to do with encodings, try reading the file in the encoding it's wirtten. – CodeCaster Jul 19 '15 at 10:26
  • which encoding i should follow. plzz give me hint so i can search google with right keyword. – Mou Jul 19 '15 at 10:52
  • You need to read the file in the encoding it's written. Very likely to be UTF-8. Any of the six linked questions show you how to read XML in a different encoding. No need to Google that, just read the questions and understand the problem you're having. – CodeCaster Jul 19 '15 at 10:57
  • thanks all. issue solved. i updated my post. – Mou Jul 19 '15 at 11:05
  • Please do not edit your question to include your solution. The correct approach here on Stackoverflow is to write an answer. – AdrianHHH Jul 19 '15 at 12:22
  • the relevant button did not come to add my answer. – Mou Jul 19 '15 at 12:23

0 Answers0