Sign in to follow this  

Need to Read Data from XML file using C#

This topic is 2537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'll keep this short.. I'm creating an Item Class that will create a new item using different data from XML files. So the item class is generic. I'm using properties in C sharp with data so what I'm going to do is inside the sets have a method that calls the XML file and read data and assigns it to whatever.... Heres an example.

The variable is HP. If the HP is negative it will be -20 and add to whatever character HP is. If it's positive then it will increase it. Anyways once the item class is creating a new instance it finds HP=100 inside the XML file and assign it inside the code to HP. So that variable will be 100 inside the code.

My problem is all the resources I find are simply to read and write using XML. But it's an entire file to display or something. I'm still new to C# and I have no experience using XML. So I mean is there any sample code around that I could examine? I have no idea what the XML file looks like inside either. XD But that's why they told me to do and well I'm trying my best.

Share this post


Link to post
Share on other sites
I typically use either the XML Serializer or the Linq-to-XML stuff.

Both pages have some code examples if you scroll down near the bottom.


Here's what I would do:

1. Write a simple Item class that you can use with the XML Serializer.
2. Write it to a file.
3. Open the file in a text editor and look at the contents to familiarize yourself with what the serializer writes.
4. Decide whether to use this approach or not.

Share this post


Link to post
Share on other sites
Ok well after examining that....I'm still a bit confused.... I think I prefer the LINQ one to serialize (simple syntax or I understand it better) anyways I'm just wondering how to use it... I mean which method to use? Create Reader method or what? XD I just want to read existing data not create anything new. Just read HP=100 then assing the number to a variable....... I'm not seeing that done in any of the examples it's just grabbing it and displaying it or what have you.

Share this post


Link to post
Share on other sites
If you use WCF DataContracts it will automatically make it easy to serialise to/from a file.

i.e.



[DataContract]
class Data
{
[DataMember]
public int HitPoint { get; set; }

public void Save(string path)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;


using (XmlWriter writer = XmlWriter.Create(path, settings))
{
DataContractSerializer dataContractSerializer =
new DataContractSerializer(typeof(ConverterConfig));
dataContractSerializer.WriteObject(writer, this);
}
}

public static Data Load(string path)
{
using (FileStream fileStream = new FileStream(path, FileMode.Open))
{
DataContractSerializer dataContractSerializer =
new DataContractSerializer(typeof(Data));
return dataContractSerializer.ReadObject(fileStream) as Data;
}
}
}
[/Source]


Share this post


Link to post
Share on other sites
If you are using Linq 2 Xml it'd be something along the lines of


class Data
{
public int HitPoint { get; set; }

public void Load(string fileName)
{
XDocument doc = XDocument.Load(fileName);

HitPoint = (int)doc.Elements("HitPoint");
}

public void Save(string fileName)
{
XDocument doc = new XDocument(new XElement("HitPoint", HitPoint));
doc.Save(fileName);
}
}


Share this post


Link to post
Share on other sites

This topic is 2537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this