tags:

views:

90

answers:

5

Hi there, I'm kind of lost about writing my information to an XML file.

I want to save both literal strings and images.

I have no idea how to write the syntax of the XML file. When I create an XML file in Visual Studio 2008 all the file has is this:

<?xml version="1.0" encoding="utf-8" ?>

So I have no clue on how to add things.

**Edit: I know what XML is, I just don't know how I can go about setting thing. I know I can search by nodes etc.

Would I do something like:

<Person>
    <Name>
        Sergio
    </Name>
    <LastName>
        Castedo
    </LastName>
</Person>

Meaning, can I write my trees anyone way I want or is there a strict way?

A: 

The great thing about XML is that you can organize it any way you want. For example:

<file>
    <image>1234567890ABCDEFG ... Hex data stream example</image>
    <caption>This is a picture of ... whatever</caption>
</file>

XML is a "human readable" way of storing data. Are you familiar with classes or structures? Both allow you to define a data-type in any way you can imagine (with whatever types are available for use). XML is similar in that you can define a new data-type (at least, that may be a convenient way to look at it).

How do you want your data stored? Is XML the best way to do that? How should the information you have be organized in a structured document? These are a few question you should answer before continuing. The example above may not express what you are thinking about but can be modified to fit your needs.

From the description of your data-storing needs, you probably can dump your strings in between the tags of your choice (whatever you decide they should be). As for the images, you might want to HEX dump the contents or base 64 encode them and dump the resulting string. Don't forget image format.

Noctis Skytower
Does any of this help you, Papuccino1? I can elaborate further if you need more help.
Noctis Skytower
Hi there, thanks for the help. I'm basically looking at XML for saving the information my program will display because it's simple and it's very suitable for the use I want it to give. Just push out the information when called upon, that's it. I'm guessing I can save just strings, but if I want to save an image, it's best I just save the locations of it in the resources correct? I just want things organized so I can use them in my program with generic methods.
Sergio Tapia
A: 

I think you should probably google "what is xml." Seriously. I'm not trying to be an ass.

aquinas
+2  A: 

XML's syntax is very similar to HTML, except the storage structure can describe the data that is stored however you prefer. A good place to start learning XML is W3School's Tutorial on XML

Jeras
...and once you have a handle on the underlying syntax, see the XmlDocument, XmlWriter and/or XDocument classes for APIs for writing data out to XML using .NET.
itowlson
Oh, and XmlSerializer provides a simple API for mapping XML documents to object graphs and vice versa.
itowlson
A: 

Try the Altova XML editor. It has a visual interface which will help you to better understand the structure of your XML file.

http://www.altova.com/xml-editor/

Mimi
+1  A: 

As others have said you dont have to write the syntax yourself. You can create an XML file pretty easily using the XmlTextWriter Class int the System.Xml namespace. For saving images you can convert the raw bytes of the image to Base64 encoded strings with Convert.ToBase64String and save them in your file.

XmlTextWriter outputWriter = new XmlTextWriter(@"C:\somefile.xml", Encoding.Default);
outputWriter.WriteStartDocument();
outputWriter.WriteStartElement("main")
outputWriter.WriteStartElement("with_attributes")
outputWriter.WriteAttributeString("attribute", "value");
outputWriter.WriteEndElement();
outputWriter.WriteStartElement("image");
outputWriter.WriteCData(Convert.ToBase64String(myByteArray));
outputWriter.WriteEndElement();
outputWriter.WriteEndElement();

Its all pretty straight forward stuff.

GrayWizardx