0

Im developing a WCF web service with receives always and only XML.

So i need to validate that input XML using their XSD. The question is, can i save them inside web service? Locally i can access XSD files via relative path into IIS Express root folder which i created manually. I tried add the XSD files in VS project but i just cant a find them on runtime.

I'm using the Shemas like this: Image1 Link

IIS XSD'd Folder Path Workaround: Image2 Link

At the moment, its working fine, the problem will be when i try deploy the service somewhere on internet.
Thank you.

tl;dr: Can i send some XSD when deploying the webservice or its just impossible?

Xplouder
  • 148
  • 1
  • 4
  • 13
  • Why do you add a screenshot for one line of code? Please use the editor to show us your code. Also, show us more, i.e. how do you construct the path to the XSD, where are they located now, etc. – BCdotWEB Jan 10 '15 at 13:06
  • Simply because somehow the question was not fitting the minimum requirements. The code is irrelevant, i added a tl;dr guess its pretty clear. – Xplouder Jan 10 '15 at 13:19
  • This might be what you are after http://stackoverflow.com/questions/17166667/validating-an-xml-against-an-embedded-xsd-in-c-sharp – ahybertz Jan 10 '15 at 13:22
  • Yes im using something similar. – Xplouder Jan 10 '15 at 13:36

3 Answers3

1

You can add the XSD as a resource, then load it from your assembly. Add the XSD to your project, and under the "Properties Explorer", set the "Build Action" to "Embedded Resource". You can then read the file with:

var schemaSet = new XmlSchemaSet();
schemaSet.Add("", XmlReader.Create(typeof(SomeClassInTheSameAssembly).Assembly
    .GetManifestResourceStream("Full.Namespace.XsdName.xsd")));

See Working with Embedded Resources or Loading XmlSchema files out of Assembly Resources for more.

Mitch
  • 21,223
  • 6
  • 63
  • 86
1

Files that belong to your solution should be physically part of it. Once that is the case you can use for instance HostingEnvironment.MapPath; or look at the answers to this question. Note that there is a possible issue with HostingEnvironment.MapPath when the WCF service is self hosted.

A possible solution is this method:

public static string MapPath(string path)
{
    if (HttpContext.Current != null)
        return HttpContext.Current.Server.MapPath(path);

    return HostingEnvironment.MapPath(path);
}

The parameter path needs to be of the format "~/XSD/MyFile.xsd", with the folder "XSD" being located in the root of your WCF service.

NEVER create folders in c:\program files (x86)\iis express.

Community
  • 1
  • 1
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35
  • Ye i knew that create the folders was a bad approach but i couldnt figure out how to solve it. Your solution is working like a charm. Thank you. :) – Xplouder Jan 11 '15 at 01:14
0

One way you can do it is by using an application setting in your config file that will hold a base file location such as:

<appSettings>
    <add key="BaseDir" value="C:\your\folder\names" />
</appSettings>

Then in your program, when you need a file, you would do something like this:

string fileLocation = System.Configuration.ConfigurationManager.AppSettings["BaseDir"] +
                           @"\your\file\location\file.xsd";

To use System.Configuration.ConfigurationManager, you will need to add a reference to System.Configuration.

Jacob Lambert
  • 7,449
  • 8
  • 27
  • 47
  • Not sure if i was clear on que question. The problem is how can i access the XSD files after the deploy. Using a relative path like your solution is just a workaround when developing locally because i wont own the server that i will export the webservice to add the folders by myself. – Xplouder Jan 10 '15 at 13:00
  • I would recommend using [Path.Combine](http://msdn.microsoft.com/en-us/library/fyy7a5kt%28v=vs.110%29.aspx) to construct paths instead of concatenating strings. – BCdotWEB Jan 10 '15 at 13:03