Dynamic XML parsing, data storage, and forms in c#

1.4k views Asked by At

So I am developing an application that I want to be able to dynamically parse an xml file, grab attributes and populate a form that is created based on what elements are present in the xml file. Then the values can be edited, and written back to the xml file that was originally opened.

I've been able to parse, save values to a database , populate forms, and write back to the original xml file via hard coding, and I think I have an idea for the dynamic "database" structure (a dictionary where the key is a node's name, and the value is a dictionary if I wanna store a node's child node information, or a string if I'm in the furthest nested child node of an element).

I'm stuck on how to grab the information I need and how to store it in the "database" structure I've come up with. I've done research and have seen people use the "dynamic" object and linq. But everything I've seen seems to involve knowing the path names they've needed before run time. Any ideas on how I should be going about actually parsing the file and grabbing data, first off? I think if I can get that figured out, the rest should kind of fall into place.

Say I have this xml

<users> <user1 age="43">John Doe</user1> <user2 age="40">Jane Doe</user2> </users>

An example database setup would look like Dictionary<users, Dictionary<user1, Dictionary<name,John Doe>

Where you could go Key("users").Key("user1").Key("name").Value to get John Doe or something like that.

Any other suggestions for my database setup would also be appreciated

2

There are 2 answers

1
Oleg On BEST ANSWER

You could parse xml using XmlDocument class. After parsing you have tree of nodes which you can traverse and show in UI. I don't understand why you want to save xml as distinct records of values in database, just save it as text. Anyway for saving xml you should translate its records back to xml.

3
Mike Hixson On

It sounds like what you are looking for is XML serialization. Before you can do that though, you need to simplify your xml. Instead of numbering the <user> elements, you will want to use an attribute that has the value for the user id. For example you can use an id attribute.

<users>
  <user id="1" age="43">John Doe</user>
  <user id="2" age="40">Jane Doe</user>
</users>

Once you have done this, create a class that you can serialize to this xml.

using System.Xml.Serialization;

[XmlRoot(ElementName = "users")]
public class Users : List<User>
{

}

[XmlType(TypeName = "user")]
public class User
{
    [XmlAttribute(AttributeName = "id")]
    public int Id { get; set; }

    [XmlAttribute(AttributeName = "age")]
    public int Age { get; set; }

    [XmlText]
    public string Name { get; set; }
}

Then all you have to do to load from the file is deserialize.

XmlSerializer serializer = new XmlSerializer(typeof(Users));
Users users = (Users)serializer.Deserialize(File.OpenRead("XMLFile1.xml"));

You can use LINQ to query the Users collection for specific users.

var user1 = users.Where(u => u.Id == 1)

Modify the User objects or add more to the Users collection, then serialize to save changes back to the file.

XmlSerializer serializer = new XmlSerializer(typeof(Users));
serializer.Serialize(File.OpenWrite("XMLFile1.xml"), users);