1

I have a class library that contains some generic proceesing functionality - call it "Engine".

I include the class library in a number of web applications.

The engine library needs an XML file as input, but the content is unique to each project.

At the moment I manually copy the XML file into each project. The engine always looks for a file in the application route.

However, I've gotten a little confused with regards to embedded resources. In order to validate the XML, I've created an XSD in my engine project and set the Build Action to EmbeddedResource.

I can't see the difference between setting the BuildAction to Content and EmbeddedResource in this case, which has led me to doubt the way that things are currently set up.

I've not a lot of experience at this level, so need some guidance. Any advice would be appreciated.

John Ohara
  • 2,821
  • 3
  • 28
  • 54

1 Answers1

1

EmbeddedResource means that the xsd is embedded inside the assembly during build, while Content means it is just copied along to the output folder. You want the embedded resource thing it sounds like.

You can access Embedded resources through code like this:

string resourceName = "SomeNameSpace.SomeFile.xsd";

Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
    if ( stream == null )
        throw new ArgumentException("resource not found", "resourceName");
    using (StreamReader reader = new StreamReader(stream))
    {
        string result = reader.ReadToEnd();
        return result;
    }
}
stuartd
  • 70,509
  • 14
  • 132
  • 163
Thomas Schmidt
  • 359
  • 2
  • 11
  • So where is the file to be parsed here? – John Ohara Dec 08 '15 at 13:15
  • The XML is stil manually copied. You only embed XSD to verify the copied XML. How would you want to embed different XML files for different projects in single assembly? – Kuba Wyrostek Dec 08 '15 at 13:28
  • I'm sorry, I'm just confused. In my mind, I have a XML file and an XSD file - the code above reads an XSD file, but what do I do with it afterwards? – John Ohara Dec 08 '15 at 13:32
  • And what do you do now? Using XSD to verify XML is a completely different question isn't it? – Kuba Wyrostek Dec 08 '15 at 13:36
  • Not really, the end result is that I still confused as to how I bring it all together. – John Ohara Dec 08 '15 at 13:43
  • 1
    If you embed the xsd in the assembly then you can load the xsd and use it to validate against the xml you copy into each web application. If the questions is "how do use a xsd to validate an xml file" then it is another question [here](http://stackoverflow.com/questions/751511/validating-an-xml-against-referenced-xsd-in-c-sharp/) If the question is how do I distribute different xml files among different web applications then there is hundreds of solutions for this and requires a new qestion i would say – Thomas Schmidt Dec 09 '15 at 07:06