Use svcutil for generating C# Classes from simple xml file - c#

I have following XML. I am trying to generate C# Classes using svcutil. I understand that there are other tools (like Visual Studio Paste Special and XML2CSharp) – but I need to see how svcutil is generating it.
<?xml version="1.0"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" templateId="2.16.840.1.113883.3.27.1776">
<title>Gc test Consultation Note</title>
<effectiveTime value="20000407"/>
<component>
<StructuredBody>
<component>
<section>
<code code="10164-2" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/>
<title>History of Present Illness</title>
<text>Test patient is a 67 year old male referred for further asthma management. Onset of asthma in his
<content revised="delete">twenties</content>
<content revised="insert">teens</content>.
He was hospitalized twice last year, and already twice this year. He has not been able to be weaned off steroids for the past several months.
</text>
</section>
</component>
</StructuredBody>
</component>
</ClinicalDocument>
I used svcutil articles and used following command – but it gave a warning saying that no code is generated.
svcutil.exe /target:code /dataContractOnly /serializer:XmlSerializer /importXmlTypes /collectionType:System.Collections.Generic.List`1 hl7sample.xml
What can done to generate C# Classes from this XML using svcutil?

Related

Serialize/Deserialize ODATA xml to C# object

I need to convert the ODATA XML to C# Object and back
Sample xml.
<entry xml:base="abc.com:8000"
xmlns=w3.org/2005/Atom"
xmlns:m="schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="schemas.microsoft.com/ado/2007/08/dataservices"
>
<id>abc.com:8000</id>
<title type="text">MaintNotifSet</title>
<content type="application/xml">
<m:properties>
<d:NotifType>MA</d:NotifType>
<d:Dscid>...dsc id...</d:Dscid>
</m:properties>
</content>
</entry>
Are there any libraries exist to help the parsing. Please throw some light.
Use XSD command tool inside visual studio installation
XSD MyXml.xml
this will generate MyXxl.XSD
then
XSD /c myxml.xsd
this will generate myxml.c which contains your classes

Reading from Settings.settings file After Obfuscation

I've recently obfuscated a DLL using Dotfuscator CE with Visual Studio 2015 Update 3. Here is the Dotfuscator.xml file I used.
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE dotfuscator SYSTEM "http://www.preemptive.com/dotfuscator/dtd/dotfuscator_v2.3.dtd">
<dotfuscator version="2.3">
<propertylist>
<property name="SourceDirectory" value="This Path Will Be Replaced By Visual Studio" />
<property name="SourceFile" value="This Filename Will Be Replaced By Visual Studio" />
</propertylist>
<input>
<asmlist>
<inputassembly>
<option>library</option>
<file dir="${SourceDirectory}\" name="${SourceFile}" />
</inputassembly>
</asmlist>
</input>
<output>
<file dir="${SourceDirectory}\" />
</output>
</dotfuscator>
Now the problem is, the obfuscated assembly contains some information stored in Settings.settings. file, when my code tries to access data from settings file it fails with this following exception.
The settings property 'ProxyTestURL' was not found
Here is the syntax to read setting:
Dim strURI As String = My.Settings.ProxyTestURL
I know there is a similar question "Dotfuscator : Error after obfuscation" but it doesn't seems to be either complete or robust, though I put my comments there too but here I would expect a second opinion and more robust solution instead of renaming my settings file literals to obfuscated names(as mentioned there).
You should exclude My.Settings from renaming. Instructions for doing so are in the docs.

Loading multiple XDocuments, and working with its documents

I wrote several lines of code but still can't get over this:
I need to load many xml docs from web library. I don't know how many documents there are so I wonder which loop should I use while loading:
XDocument doc = XDocument.Load("http://" + i);
where -i is identifiers number.
I tried loading until i get document without meaningful content (thought it is the end, the rest are empty), but problem is that there is several Xdocs that are empty in the middle of library.
XML with content looks like
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
<GR>
<header>
<identifier>53,number of doc...used for counting</identifier>
</header>
<metadata>
<P xmlns="" xsi:schemaLocation="">
<TITLE>title</TITLE>
<CERTIFICATE NAME="different names">
</CERTIFICATE>
<YEAR>
<DATE>2012-10-18T00:00:00Z</DATE>
</YEAR>
<MINIATURE>
<COPY>
<CNAME>Copy name<CNAME>
<FORMAT>obj/max/dxf/3ds/...</FORMAT>
</COPY>
</MINIATURE>
</metadata>
</GR>
</OP>
XML without content
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
Furthermore, I need to do some counting like:
Tot.no. of doc,
No. of docs per certificate <CERTIFICATE>
No. of docs for each year <YEAR><DATE>
No of docs for each format <MINIATURE><COPY><FORMAT>
and my output should look like:
<?xml version="1.0" encoding="UTF-8" ?>
<Statistic>
<DocSum>21220</DocSum>
<Certificates>
<Certificate id=”certificateName”>17098</Certificate>
…
<Certificates>
<Years>
<Year year=”2014”>23</Year>
…
</Years>
<Miniature>
<Format post=”obj”>11723</Format>
…
</Miniature>
</Statistic>
If you could give me some help, hints or tips how to deal with it.
The posted answer by smink to the following thread should get you on the right path.
C# HttpWebRequest command to get directory listing
One of the easiest ways to get a list of the files of a web directory without knowing exactly how many there are or their filenames is by parsing the html of the directory and pulling out the tags.
You can then iterate through these tags and filter them out for the files by extensions that you need. I can provide a more in-depth example if necessary.

Adding an XML file to your XNA project

I'm creating an XNA game. I've made it so I can specify all the level details in an XML file which is then de-serialized and used to set up the level details.
At the moment, it's just referencing a file on my computer - my question is, how do I reference this more generically?
Adding the xml in my content folder created a multitude of complaints about schemas and such like, which made me think that likely wasn't the correct route.
Any suggestions?
I tried removing all the entries from the XNA, this gives:
Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)
EDIT:
The xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type = "RDrop.Level[]">
<Item>
(stuff)
</Item>
<Item>
(stuff)
</Item>
</Asset>
</XnaContent>
EDIT:
I've started a new windows phone project - the previous one wasn't one. I've copied everything over and added "dataTypes" ala this tutorial:
http://msdn.microsoft.com/en-us/library/ff604979.aspx
Game project references -> content, MyDataTypes.
Content references -> MyDataTypes.
The XML is as is in previous edit and is contained in the content folder via Add-> Existing Item-> Level.XML.
Any ideas?
You can leave the build action as "Compile". One method to do what you want is the following:
Create a class that the xml is going to be describing. Example: Level.cs
Then structure your xml file like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type="The_Level_class_namespace.Level">
<Property1>Value</Property1>
<Property2>Value</Property2>
<Property3>Value</Property3>
<Property4>Value</Property4>
</Asset>
</XnaContent>
if you want the xml to describe an array of objects you can do structure the xml like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type="The_Level_class_namespace.Level[]">
<Item>
<Property1>Value</Property1>
<Property2>Value</Property2>
<Property3>Value</Property3>
<Property4>Value</Property4>
</Item>
</Asset>
</XnaContent>
From there you just need to make sure your values are in the proper format. For example a vector2 object would be like this:
<Vector2Property>x_value y_value</Vector2Property>
Make sure that your content project references the game project or library project.
Hope this helps :)
Open the properties of your XML document (right click in your content folder). You can set the Build Action to : None.
That way, the compiler won't analyse your schema, thus it won't produce any warnings.
(I'm not entirely sure about this, just my first guess)

Could not find schema information for the element 'castle'

I'm creating a Custom tag in my web.config. I first wrote the following entry under the configSections section.
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor" />
But, when I try to create a castle node inside the configuration node as below
<castle>
<components>
</components>
</castle>
I get the following error message:"*Could not find schema information for the element '**castle'*." "***Could not find schema information for the element '**components'***."
Am I missing something? I can't find why. And, if I run the application anyway, I get the following error "Could not find section 'Castle' in the configuration file associated with this domain."
Ps.// The sample comes from "Pro ASP.NET MVC Framework"/Steven Sanderson/APress ISBN-13 (pbk): 978-1-4302-1007-8" on page 99.
Thank you for the help
============================================================
Since I believe to have done exactly what's said in the book and did not succed, I ask the same question in different terms. How do I add a new node using the above information?
=============================================================================
Thank you. I did what you said and do not have the two warnings. However, I've go a big new warning:
"The element 'configuration' in namespace 'MyWindsorSchema' has invalid child element 'configSections' in namespace 'MyWindsorSchema'. List of possible elements expected: 'include, properties, facilities, components' in namespace 'MyWindsorSchema'."
What you get is not an error that will prevent you from running your application. It is just a warning that Visual Studio emits because it does not know the castle node in a config file. You could use a schema to enable intellisense. Download the Castle Windsor Schema file and take a look at the readme.txt inside. It tells you to put windsor.xsd somewhere on your hard drive and then reference it in the config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="MyWindsorSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="MyWindsorSchema file://S:\Common\Windsor\windsor.xsd">
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
</components>
</castle>
</configuration>

Categories