Sergio Tapia
Sergio Tapia

Reputation: 41218

How do I write my information inside of an XML file?

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?

Upvotes: 0

Views: 221

Answers (5)

GrayWizardx
GrayWizardx

Reputation: 21241

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.

Upvotes: 1

Mimi
Mimi

Reputation: 21

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/

Upvotes: 0

Noctis Skytower
Noctis Skytower

Reputation: 22041

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.

Upvotes: 0

Jeras
Jeras

Reputation: 138

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

Upvotes: 2

aquinas
aquinas

Reputation: 23796

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

Upvotes: -1

Related Questions