tags:

views:

82

answers:

2

Hi, I'm trying to create a VB.net application which reads each line from an XML-file (or ini file, doesn't matter) which dictates what the name of a string should be.

E.g

"string1 = xml.line1"
"string2 = xml.line2"

etc

What would be the best approach of doing this? I've already pluckered a bit with Xml.XmlTextReader and it seems to do the job, but I can't manage to break down each line as it's own string?

A: 

If you are dealing with the XML line by line and want to ignore XML semantics you should treat it as a plain text file and run a regular expression or something on each line:

Dim lines = File.GetAllLines("file.xml")
' Check the lines (which is a String array) for the criteria
Mehrdad Afshari
+1  A: 

A line is rarely a useful concept in an XML document. For example:

<foo>
    <bar first="x"
         second="y" />
</foo>

is equivalent to

<foo>
    <bar first="x" second="y" />
</foo>

Normally you'd query an XML document for individual elements rather than lines. A line would be much more sensible in a .ini file, however.

What's the bigger picture here? What are you trying to achieve?

If you need to read all the lines of a text file (but you don't need them all in memory at once) I'd recommend using File.OpenText which returns a TextReader, then calling ReadLine() and processing the strings as you go until the line returned is Nothing (which means you've reached the end of the file). Don't forget to wrap the reader in a Using statement.

EDIT: If you don't need anything else in the file, then I'd just write one entry per line and you're basically done. On the other hand, that means if you ever need any other configuration information, you'll need to change everything.

Using an ini-file would be fairly simple, but you'd have to write the code to parse it. (There may be 3rd party libraries around to do it for you, admittedly. I wrote one at a previous company, and it didn't take very long.)

Using an XML file would introduce a bit more "fluff" around the values, but it would be quite simple to read, particularly if you're using .NET 3.5 and LINQ to XML. For instance, you could have a file like this:

<Configuration>
    <RegistryEntries>
         <RegistryEntry>First</RegistryEntry>
         <RegistryEntry>Second</RegistryEntry>
         <RegistryEntry>Third</RegistryEntry>
    </RegistryEntries>
</Configuration>

and get the sequence of entries just by doing:

document.Root.Element("RegistryEntries")
             .Elements("RegistryEntry")
             .Select(e => e.Value);

As yet another alternative, you could use the built-in configuration/settings features of .NET. Have you tried using the "Settings" part of project properties?

Jon Skeet
The values of these lines should represent the name of a registry entry. Eg, name, email etc. And I don't want to hardcore the names of the registry entries in case they change. Perhaps reading from an ini file would be better, heck even a text file could work. :)
Kenny Bones