Wish
Wish

Reputation: 1614

Need help writing and reading XML file in C#

Idea - List of vertices(Key, X, Y, Priority to store).

   <?xml version="1.0" encoding="utf-8"?>
   <Vertices>
    <Vertex Key="0" X="149" Y="209" Priority="7" />
    <Vertex Key="1" X="278" Y="128" Priority="7" />
   </Vertex>

Is this valid XML? It keeps saying me that root element is missing, when i try to open it... If so, can someone provide a valid c# XDocument code to open this file ?

Upvotes: 0

Views: 278

Answers (2)

jb.
jb.

Reputation: 10351

You are opening <Vertices> but closing </Vertex>. Need to change that last closing tag to </Vertices>

Side note:

If you load an XML file into Visual Studio it will tell you if it is invalid XML and why. For this example it gave the errors:

Error   1   Tag was not closed. XMLFile1.xml    Line 2  Column 5
Error   2   Expecting end tag </Vertices>.  XMLFile1.xml    Line 5  Column 6    

If you do not own Visual Studio, you can download the Express version for free and get the same functionality.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160992

It's not valid XML - your closing element has the wrong name - this would be valid:

<?xml version="1.0" encoding="utf-8"?>
<Vertices>
  <Vertex Key="0" X="149" Y="209" Priority="7" />
  <Vertex Key="1" X="278" Y="128" Priority="7" />
</Vertices>

Also make sure that if you are loading an XML file you use XDocument.Load and not XDocument.Parse.

Upvotes: 2

Related Questions