1

I have an xml file.

This file has attributes and the values are in Cyrillic.

How I can read this xml file?

Ex Xml:

<Ships>
    <Ship X="3" Y="Г" Length="3" Orientation="vertical" />
    <Ship X="7" Y="А" Length="2" Orientation="horizontal" />
    <Ship X="10" Y="Ж" Length="1" />
</Ships>  
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Zhenia
  • 3,939
  • 3
  • 15
  • 15
  • 6
    welcome to stackoverflow: what have you tried? – Daniel A. White Nov 02 '12 at 13:31
  • Use the XMLReader: http://stackoverflow.com/questions/2441673/reading-xml-with-xmlreader-in-c-sharp – Joe Nov 02 '12 at 13:31
  • What's wrong with `[XmlDocument.Load]`(http://msdn.microsoft.com/en-us/library/875kz807.aspx)? – Anri Nov 02 '12 at 13:32
  • I would look into XML serialization. As for the characters you're trying to support... you may need to base64 encode them or something. I'm not 100% sure since I have not worked with much outside of the english alphabet. Check out http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization for an XML Serialization tutorial. After reading that look up `XmlAttribute`, `XmlArray` and `XmlArrayItem` – Frito Nov 02 '12 at 13:34
  • Please try in google. Try this string path = "XML path"; DataSet ds = new DataSet(path); – Prasad Kanaparthi Nov 02 '12 at 13:34
  • Are you having trouble reading the file? Or trouble with character encoding? – Jeff B Nov 02 '12 at 13:35
  • 3
    Yes, as long as the file is stored in unicode format like UTF-8 (fairly standard) or UTF-16, UTF-32 it can be read in exactly the same manner as a file with just latin characters. – mortb Nov 02 '12 at 13:35
  • Add an XML header with UTF-8 encoding. http://stackoverflow.com/questions/7007427/is-a-valid-xml-file-required-xml-header-declaration-xml-version-1-0-encodin – C.M. Nov 02 '12 at 13:39
  • 2
    Just get your encoding right and it will work. – erikkallen Nov 02 '12 at 13:44

3 Answers3

1

How about using Linq To Xml

var xDoc = XDocument.Parse(xmlstring);//or XDocument.Load(filename);
var ships = xDoc.Descendants("Ship")
                .Select(s => new
                {
                    X = (string)s.Attribute("X"),
                    Y = (string)s.Attribute("Y"),
                    Orientation = (string)s.Attribute("Orientation"),
                    Length = (string)s.Attribute("Length"),
                })
                .ToList();
L.B
  • 114,136
  • 19
  • 178
  • 224
  • When I use XDocument.Load(filename); - I have XmlException - Invalid character for the specified encoding. – Zhenia Nov 02 '12 at 13:48
  • @user1794316 you need to make sure that the file is saved as UTF-8. – Adam Nov 02 '12 at 13:52
  • @user1794316 you can load the xml by giving the correct encoding `var xDoc = XDocument.Load(new StreamReader("filename", Encoding.GetEncoding("xx-XX")));` – L.B Nov 02 '12 at 13:54
0

use xsd.exe as already described here

you can find it under

C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\xsd.exe

or

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe

Community
  • 1
  • 1
Luke94
  • 712
  • 5
  • 14
0

This just worked for me:

        var doc = new XmlDocument();
        XmlReader reader = XmlReader.Create(new StreamReader(@"..\..\filename.xml", Encoding.UTF8));
        doc.Load(reader);

        var ships = doc.SelectNodes(@"//Ship");
C.M.
  • 1,474
  • 13
  • 16