I am using Microsoft Visual Studio.
I am using Paste Special
I am using Past XML as Classes.
The XML I have is XML another program generates.
I can force meta data into the XML. (i.e. Say when a value of 123 is a string or a number)
Can I/How do I communicate this information to XSD or the IDE?
Related
My question is : How to deserialize unknown XML to C# classes ? I know about "Paste special -> Paste XML as Classes" but i need something (code )which generate a C# classes object from XML. My program need add, remove and edit all nodes in tree XML.
You can do this by using XDocument, parsing the XML, outputting a C# CodeDom into memory, compiling it in memory. Not sure why you would want to do such a thing, but XML Parsing and CodeDom is the way to go to.
Is it possible to store an actual XML file in a Visual Studio 2010 project and then refer to it in code?
ie. Can I store an XML file (see fragment below) and then refer to data elements within the XML data? How can this be done and how would you refer to data elements deep within the XML data using C# or VB code?
If the XML file is added to the project, will it be embedded into the DLL or EXE when compiled? If not, how can it be embedded into the DLL or EXE?
<readReferenceDataResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<readReferenceDataResult xmlns="http://test.website.com/hi/xsd/providermessages/ReadReferenceData/5.6.9">
<elementReferenceValues>
<elementName>organisationTypeCode</elementName>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">1678</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Accupuncture and Eastern Medicine</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">6454</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Technical and Laboratory Services</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">9782</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Naturopathy and Natural Health</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9>8557</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Aged Care Services</referenceDescription>
Just add a resource file (.resx) to your project if one is not already present. Select File as the resource type and drag your XML file into that and give it a name (if you don't like the automatically generated one).
This will create an embedded resource type which you can then access in code by the automatically generated class representing the resource, so for example if your project is called Foo and you add a resource file called Resources and add an XML file resource called MyData, then you can access that resource at Foo.Resources.MyData in code. This will expose the contents of the file as a string, which you can then load into an XmlDocument object when you need to by using XmlDocument.LoadXml.
By the sounds of it, the best solution I can think of would be using XmlDocument
You use the XmlDocument to hold the XML file you want to read and then you can retrieve the elements/nodes from there.
If you wanted something a bit faster you can also use Linq to XML, which is nice and simple to use.
Finally, if you have memory constraints/don't want to load the whole document into memory to read it every so often you can use XmlReader. Which allows you to read it directly from file, node by node. It is however forward only, and is a bit tricky to use.
I am trying to come up with a windows form application (or WPF) developed in C#.The requirement for my app is to get user search related xml node data from a website containing xml. The application would connect to a website containing xml and grab relevant xml nodes from the website. I would then display the xml node data on my windows app. What's the best way to do this, also an extension would be to grab all the xml and store in a data tier.
An sample website I will be similar to this page
http://www.amk.ca/quotations/sherlock-holmes.xml
Not entirely sure what your questions is - are you asking how to achieve this (downloading XML), or where to best put it, or what?
To grab the XML, use something like this:
using System.Net;
WebClient client = new WebClient();
string result = client.DownloadString("http://www.amk.ca/quotations/sherlock-holmes.xml");
You get back a string of XML, which you can now parse using XmlDocument or XDocument (Linq-to-XML) - are you asking how to do this??
Or if you know what sites and what format XML you're hitting ahead of time, you could also download the XML and generate a XML schema from it, and in a second step generate C# classes from the XML schema that would be suitable for deserializing the XML string into an enumeration of e.g. Quotation classes (based on the <quotation> tag in the sample XML provided).
Update: if you have a sample XML as a file, you can use the xsd.exe command line utility to generate a XML schema from the XML, and based on that XML schema, you can create a C# class to be used for deserialization. See the MSDN docs for xsd.exe for more details.
Basically, calling xsd.exe (yourfile.xml) will generate a yourfile.xsd XML schema based on your XML input file, and running xsd.exe /c (yourfile.xsd) will generate a C# class from that XML schema.
Using that, you could deserialize your XML into a C# class in one step and then "explore" the contents of the XML by just navigating around the C# class, its properties, and its lists of subelements.
That deserialization would look something like this:
XmlSerializer deserializer = new XmlSerializer(typeof(ThatDataTypeGenerated));
object result = deserializer.Deserialize(<either a file name, or a stream or something>);
This works as long as you know ahead of time what XML type you'll be getting (so that you can generate the XML schema and C# class from it, ahead of time).
Also, you can do the first step (turn XML data file into schema) inside Visual Studio, too (menu "XML" -> "Generate XML schema"), and for the second step (turning the XSD XML schema into a C# class), you could have a look at something like Xsd2Code.
My C#/.NET application reads XML files that are manually edited by the users. The allowed elements and tags are described in the application's documentation. I'm using LINQ to extract data from the XML file.
Before extracting data from the XML file, I'd like to validate it to see if it has the expected structure. If not, it would be nice to have information about what is wrong so that I can give some feeback to the user.
What's the simplest way to do this in C#?
You can validate xml files against XSD.
First you have to create Xml Schema Definition file. See example
use XML Schema Definition Tool to create XSD from XMLfile
Use this code to validate input XML using corresponding XSD
Hope this will help...
EDIT
This article explains all possible ways to validate xml, using C#
How To Validate an XML Document by Using DTD, XDR, or XSD in Visual C# .NET
IMO best option is to use XSD.
Validating Input Xml Data Files
I need a way to store data inside xml files and write to differant parts of the file, as well as add elements and structure to the xml document.
I need full control over the file names and xml documents, and it would be much easier if I could use some kind of SQL layer to read and write from the xml.
Just due to project constraints I am tied into using XML, but if possible would like a trust and tested open source solution for this.
Or should I be using out of box .net functionality for this?
you should be using out of box .net functionality.
the XML namespaces and Linq-to-XML will do this for you.