-2

I have a list which is of type ListItem

The ListItem is a custom class I made and it looks like following:

public class ItemList
{
    public string ItemID { get; set; }
    public int QuantitySold { get; set; }
    public string GalleryURL { get; set; }
}

I need to store this List into a folder on my project which looks like this:

/Helpers/CachedResults/FileGoesHere

And so that I can reconstruct that list each time whenever I need it back in code...

16
  • 1
    Perhaps XML serialization? stackoverflow.com/questions/4123590/serialize-an-object-to-xml Commented Nov 20, 2016 at 20:20
  • This could be it, could you show me a practical example with my case ? :) Commented Nov 20, 2016 at 20:27
  • Guys I don't want to serialize it to XML... the code before the list is built I actually converted the XML file into this list.. Now I need to just store it into some kind of file so that I can rebuild it when needed... Commented Nov 20, 2016 at 20:28
  • Maybe bytes array or something like that?? Something simple and easy so that I can rebuild that file back to this original list :D Commented Nov 20, 2016 at 20:29
  • 1
    I fail to see the problem. Commented Nov 20, 2016 at 20:46

2 Answers 2

3

I would do this with LINQ to XML.

To use it, you need to add a reference to System.Xml.Linq (in VS, right-click References -> Add Reference -> check System.Xml.Linq).

You don't need to convert the List into an array, but of course, you can, if you want. That's done simply with the list.ToArray() method.

The writing-to-file code would be:

List<ListItem> list = new List<ListItem>();

// (the list is populated)

XDocument xDocument = new XDocument(); // create empty document
XElement root = new XElement("list"); // add root element, XML requires one single root element
xDocument.Add(root); // add root to document
foreach(var listItem in list) 
{
    var xElement = new XElement("list-item", // <list-item />
        new XAttribute("id", listItem.ItemID), // id="id"
        new XAttribute("quantity-sold", listItem.QuantitySold), // quantity-sold=5
        new XAttribute("gallery-url", listItem.GalleryURL) // gallery-url="foo/bar"
    );
    root.Add(xElement); // add list-item element to root
}

xDocument.Save("Helpers/CachedResults/File.xml"); // save file

Note that the directory must exist for Save to work!

Then parsing:

List<ListItem> list = new List<ListItem>();

XDocument xDocument = XDocument.Load("Helpers/CachedResults/File.xml"); // load from file
XElement root = xDocument.Element("list"); // get root element

foreach (XElement xElement in root.Elements("list-item")) // traverse through elements inside root element
{
    list.Add(new ListItem // add list items
    {
        ItemID = xElement.Attribute("id").Value, // parse id
        QuantitySold = Int32.Parse(xElement.Attribute("quantity-sold").Value), // parse int for QuantitySold
        GalleryURL = xElement.Attribute("gallery-url").Value // parse url
    });
}

And we're done!

Sign up to request clarification or add additional context in comments.

5 Comments

It's basically manual XML serialization and unserialization. You'll get the same result. A perfectly good answer.
Well you did write the code nicely, I gotta tell you that... But this isn't the way to go since I actually just before thist List<ListItem> is created, I did exactly parse the XML document that I got from ebay and converted it to this list of items :D... This would be a step-back or redoing everything I actually did XD
Why would you need to redo your "ebay code" though?
Here's the thing, I get the response from ebay for specific items (ID's) that I pass through the XML request to ebay... ebay returns me the results in the form of XML file. I convert the XML responses I got from ebay into this List<ReusltItem> and now I'm trying to save that list onto the file system so that I can re-use it to display the result to the end user (sorta way of caching the results)...
Sounds ok to me, but I still fail to see what you would need to recode in this process.
0

According to your comment:

Now I need to just store it into some kind of file so that I can rebuild it when needed...

you just need the following lines:

File.WriteAllText(path, xml);
xml = File.ReadAllText(path);

where xml is the string you get from serializing / the serialized string that you stored previously and want to deserialize)

(This is besides the actual serialization that I mentioned in my comment: Serialize an object to XML)

EDIT

From the comments, it seems like your trying to parse xml that you receive. Then perhaps you should consider using the classes that were made for that. See Parsing XML using XDocument .

8 Comments

what is "xml" in this answer?
It could be the list of responses that I received from eBay... Hold on I'll try this... It might be the solution
@ispiro is there any way to read it line by line ?? Since every line I'll write in is basically an XML document for itself lol..
@User987 Is File.ReadAllLines better?
@Kinetic updated the answer with that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.