7 Replies - 2780 Views - Last Post: 07 December 2011 - 03:41 PM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 02:29 PM

I'm using HtmlAgilityPack to parse html from a website. Yada yada yada I have a List<string> ready to save to an xml file in my project.

That XML file is set as an Embedded Resource as I don't want anyone to easily copy my parsed information.

How can I save this information to this XML file?

To READ from an Embedded Resource, I've done this and it works:

XDocument championXml;
 
var asm = Assembly.GetExecutingAssembly();
using (var stream = asm.GetManifestResourceStream("LinqToXmlDemo.Champions.xml"))
{
    using (XmlReader reader = XmlReader.Create(stream))
    {
        championXml = Xdocument.Load(reader);        
    }
}



But I'm stuck on how to write information on a stream.

How can I save my List<string> strings into an XML in my project?

Ideally, I'd like the XML to look like:

<urls>
    <url>URL here...</url>
    <url>URL here...</url>
    <url>URL here...</url>
    <url>URL here...</url>
    <url>URL here...</url>
</urls>


This post has been edited by Sergio Tapia: 07 December 2011 - 02:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Saving a List<string> to an XML file set as embedded resource.

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 02:39 PM

It seems the XmlTextWriter class supports streams. Will post my findings...
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 02:47 PM

This is how I did it.

List<string> data = new List<string>();
data.Add("test");
data.Add("test2");

XElement root = new XElement("urls");
foreach(var s in data)
{
    XElement ele = new XElement("url", s);
    root.Add(ele);
}

root.Save(@"C:\testing.xml");



You could alternatively use the XmlTextWriter class.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,751
  • Joined: 08-June 10

Re: Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 03:12 PM

I think you might have missed the question there, eclipsed4utoo. It seemed to me he's asking about how to save it back to the stream.

If that's the question, the answer is "you can't." You can't modify the running executable. If I were you, I'd skip the resource and go with a DB like SqlCE or Sqlite, and possibly encrypt the data being stored, if you really want that protection.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 03:17 PM

Curtis Rutland

This should work...

List<string> data = new List<string>();
data.Add("test");
data.Add("test2");

XElement root = new XElement("urls");
foreach(var s in data)
{
	XElement ele = new XElement("url", s);
	root.Add(ele);
}

root.Save(asm.GetManifestResourceStream("LinqToXmlDemo.Champions.xml"));



I didn't test it, but the XElement.Save method accepts a Stream as a parameter to save to.

EDITED: Tested the code above and it worked writing to a normal file on the file system. Assuming it would do the same to an embedded resource.
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 03:22 PM

I just came back to the thread and figured out as much. I can't save to this XML file because it's being embedded into the compiled binary.

"Cannot write to stream" errors abound. :)

Hm... I guess I'll just write to a regular old XML file without embedding it. I guess I could then write to it.

So figure I add a .xml file to my solution and name it Foo.xml - how would I access it? The XmlTextWriter class need a path to it, and since the path changes, I can't hard code this. I'll need to get the path of the running executable and then append the name of the xml, correct?

Using reflection, how do I get the path of the running executable?
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,751
  • Joined: 08-June 10

Re: Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 03:35 PM

View Posteclipsed4utoo, on 07 December 2011 - 04:17 PM, said:

Curtis Rutland
I didn't test it, but the XElement.Save method accepts a Stream as a parameter to save to.

EDITED: Tested the code above and it worked writing to a normal file on the file system. Assuming it would do the same to an embedded resource.


Streams obtained from the assembly via GetManifestResourceStream are read-only, as proved by this code:

var asm = Assembly.GetExecutingAssembly();
using (var stream = asm.GetManifestResourceStream("Namespace.filename.extension")) {
    Console.WriteLine(stream.CanWrite);
}
Console.ReadKey();



They can't be written to or modified.

@Sergio Tapia, you don't need to bother getting the current path. When you don't provide an absolute path to a file, it assumes a relative path and looks in the directory of the executable. What you do need to do is change your XML File from "Embedded Resource" to "Content", and change the Copy to Output Directory setting to "Copy if newer".

I did this with a file named "terms.xml", and the following code works:

const string path = @"terms.xml";
var element = XElement.Load(path);
Console.WriteLine(element);


Was This Post Helpful? 2
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Saving a List<string> to an XML file set as embedded resource.

Posted 07 December 2011 - 03:41 PM

Good to know, I didn't know I could reference paths like that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1