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.
Related
If I have a XSD and want to make sure it follows all the standards so that it creates a valid XML. How would that be done? The only sources I can find is back checking the XML to the XSD.
I want to deserialize special characters. This is the sample XML tag:
<Address1>125*1(&()*)1798</Address1>
How can I do that using XmlSerializer? I want to do this in the code in c# rather than changing the contents of the Xml because the file is being uploaded by the user. I can't ask the user to fix the contents in the XML. I want to rather handle this in C# code.
Any help is appreciated.
How to upload XML file and generate Html file without XSLT file using C#...
LINQ to XML is one option. See also: Generating HTML using LINQ
The question is a little open-ended, but you might consider just writing a program that:
Deserializes the XML.
Having read in the XML, format the text.
Output the text using the standard in/out libraries in C#.
I don't know if this is the most efficient solution -- but I think it would work.
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