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
Related
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?
I have an XML file which contains 1) a serialized object and 2) a serialized hashtable. Here is my pseudo xml file(data.xml):
//PROPERTIES of an object called "Information"
<?xml version="1.0" encoding="utf-8"?>
<Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CompileScript>false</CompileScript>
<SaveScript>false</SaveScript>
<ConvertPaths>false</ConvertPaths>
<UseUninstaller>true</UseUninstaller>
<UninstallSuccess>[NAME] was successfully removed from your computer. </UninstallSuccess>
<UninstallPrompt>Are you sure you want to completely remove [NAME] and all of its components?</UninstallPrompt>
<AllowChangeStartMenuFolderName>false</AllowChangeStartMenuFolderName>
//... (many others)
//Here starts KEYS & VALUES of my hashtable
<entry>
<fileName> My file name 1</fileName>
<instDir>My file location 1</instDir>
</entry>
<entry>
<fileName> My file name 2</fileName>
<instDir>My file location 2</instDir>
</entry>
//...and so on.
My goal is to deserialize half of the XML file (up to "AllowChangeStartMenuFolderName") back to "Information" object and the remaining part back to a hashtable. My concerns:
How do I tell the program to deserialize the file up to "AllowChangeStartMenuFolderName" and then STOP?
How do I tell the program to start deserializing at a certain point of an XML file(starting from the "entry" tag and down to the end of the file)?
Can somebody at least put me in the right direction, I haven't found any information on the internet. Thanks
EDIT
I have tried :
XmlTextReader reader = new XmlTextReader("...Path...\\data.xml");
//the last element before the keys and values of a hashtable start
reader.ReadToDescendant("AllowChangeStartMenuFolderName");
XmlSerializer serializer = new XmlSerializer(typeof(Classes.Information));
//info is a new empty object which should be filled with values from the XML file
this.info = (Classes.Information)serializer.Deserialize(reader.ReadSubtree());
reader.Close();
Throws me an exception at the last line - "Error in XML Document (27,4)".
My original XML (the first one was pseude code):
<?xml version="1.0" encoding="utf-8"?>
<Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CompileScript>false</CompileScript>
<SaveScript>false</SaveScript>
<ConvertPaths>false</ConvertPaths>
<UseUninstaller>true</UseUninstaller>
<UninstallSuccess>[NAME] was successfully removed from your computer.</UninstallSuccess>
<UninstallPrompt>Are you sure you want to completely remove [NAME] and all of its components?</UninstallPrompt>
<AllowChangeStartMenuFolderName>false</AllowChangeStartMenuFolderName>
<StartMenuShortcut>true</StartMenuShortcut>
<StartMenuUninstallIcon>true</StartMenuUninstallIcon>
<TheRightProgramFilesDirectoryUserView>Program Files (x86)</TheRightProgramFilesDirectoryUserView>
<RadioButtonRadioButton>false</RadioButtonRadioButton>
<CheckBoxRadioButton>false</CheckBoxRadioButton>
<ClassicButtonRadioButton>false</ClassicButtonRadioButton>
<AllowChangeDirectoryCheckBox>true</AllowChangeDirectoryCheckBox>
<ApplicationDirectory>C:\Program Files(x86)\ANNAX\My application</ApplicationDirectory>
<ApplicationName>My application</ApplicationName>
<ApplicationVersion>1.0</ApplicationVersion>
<ApplicationPublisher>My company, Inc.</ApplicationPublisher>
<ApplicationWebsite>http://wwww.mycompany.com</ApplicationWebsite>
<SetupIcon>Icons\modern-install.ico</SetupIcon>
<GermanCheckBox>false</GermanCheckBox>
<EnglishCheckBox>true</EnglishCheckBox>
<FrenchCheckBox>false</FrenchCheckBox>
<ThirtyTwoBitRadioButton>true</ThirtyTwoBitRadioButton>
<SixtyFourBitRadioButton>false</SixtyFourBitRadioButton>
<entry>
<fileName>file1</fileName>
<instDir>instDir1</instDir>
</entry>
<entry>
<fileName>file2</fileName>
<instDir>instDir2</instDir>
</entry>
<entry>
<fileName>file3</fileName>
<instDir>instDir3</instDir>
</entry>
<entry>
<fileName>file4</fileName>
<instDir>instDir4</instDir>
</entry>
<entry>
<fileName>file5</fileName>
<instDir>instDir5</instDir>
</entry>
<entry>
<fileName>file6</fileName>
<instDir>instDir6</instDir>
</entry>
<entry>
<fileName>file7</fileName>
<instDir>instDir7</instDir>
</entry>
<entry>
<fileName>file8</fileName>
<instDir>instDir8</instDir>
</entry>
<entry>
<fileName>file9</fileName>
<instDir>instDIr9</instDir>
</entry>
I'm currently trying to get rid of OData error which looks like a problem with decimal parsing/deserializing.
I have working OData API (using System.Web.Http.OData library) in my ASP.NET MVC application and user can download OData Feed file *.odc pointing to said OData API. When there is a decimal in OData API response, such as
<d:Value m:type="Edm.Decimal">50.50</d:Value>
and user wants to open OData Feed file in Excel (tested on Excel 2016) to fetch data he gets an error:
We couldn't get data from the Data Model. Here's the error message we got:
The following system error occurred: Type mismatch.
What is intersting is that there is no error when the decimal value has no decimal part, like this:
<d:Value m:type="Edm.Decimal">50.00</d:Value>
Here's complete API response (with urls cut out):
<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="xxxxxxxxx" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<id>http://schemas.datacontract.org/2004/07/</id>
<title />
<updated>2016-06-22T11:01:10Z</updated>
<link rel="self" href="xxxxxxxxxxxxx" />
<entry>
<id>xxxxx</id>
<category term="Itm.Erp.Domain.Finance.CashRegistryListView" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="xxxx" />
<link rel="self" href="xxxx" />
<title />
<updated>2016-06-22T11:01:10Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Number>001</d:Number>
<d:Location m:null="true" />
<d:Direction>In</d:Direction>
<d:Person>Sam</d:Person>
<d:Company>Company</d:Company>
<d:Date m:type="Edm.DateTime">2016-06-06T00:00:00</d:Date>
<d:IsProcessed m:type="Edm.Boolean">true</d:IsProcessed>
<d:ExpenseType m:null="true" />
<d:Value m:type="Edm.Decimal">50.50</d:Value>
<d:EffectiveValue m:type="Edm.Decimal">50.50</d:EffectiveValue>
<d:Seller m:null="true" />
<d:DocumentNumber m:null="true" />
</m:properties>
</content>
</entry>
</feed>
What is the problem with decimals in API response and is there a way to fix it? Does anyone had a similar problem and managed to solve it?
I am writing a cloud service and using ASP.NET web role with WebForm.
In my code I get data in XElement and now I want to extract data from it and display it in table or grid format on WebForm
My XElement contains few <entry> tags like the following:
<entry xml:base="https://STORAGE_ACCOUNT.table.core.windows.net/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="W/"datetime'2013-09-08T07%3A19%3A07.2189243Z'"" xmlns="http://www.w3.org/2005/Atom">
<id>https://STORAGE_ACCOUNT.table.core.windows.net/authors(PartitionKey='Beckett',RowKey='Molloy')</id>
<title type="text"></title>
<updated>2013-09-08T07:19:07Z</updated>
<author>
<name />
</author>
<link rel="edit" title="authors" href="authors(PartitionKey='Beckett',RowKey='Molloy')" />
<category term="STORAGE_ACCOUNT.authors" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:PartitionKey>Beckett</d:PartitionKey>
<d:RowKey>Molloy</d:RowKey>
<d:Timestamp m:type="Edm.DateTime">2013-09-08T07:19:07.2189243Z</d:Timestamp>
<d:Artist>Beckett</d:Artist>
<d:Title>Molloy</d:Title>
</m:properties>
</content>
</entry>
and I want to extract the following tags
<d:Artist>Beckett</d:Artist>
<d:Title>Molloy</d:Title>
and display data in tabular format on ASPX webform like below
Artist Title
Beckett Moelly
How can I do this in my code?
I saw some examples of binding to Dataset but it works with xml file on some drive but for I have it in my code. I also saw people suggesting using XSLT to convert XML to HTML and then display it but I do not know how to do that in code. Please provide me pointers
Well, assuming you did want to do this using LINQ to XML rather than one of your other options, it's pretty simple:
XNamespace atom = "http://www.w3.org/2005/Atom"
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"
foreach (var entry in element.Descendants(atom + "entry"))
{
var artist = (string)entry.Descendants(d + "Artist").Single();
var title = (string)entry.Descendants(d + "Title").Single();
// ... do something with these values
}
I have an XML file which has been exported from Orchard CMS, what I need to now do is convert the nodes within the XML file to a new structure so that I can import the file in to Umbraco.
How do I go about doing this? I'm thinking I could write some c# .net to read the XML file and then make the changes I need and then same it as a new file.
Example of a what I am trying to do is:
Exported file:
<BlogPost Id="/alias=The Blog\/2012\/09\/10\/on-starters-orders" Status="Published">
<TextField.Excerpt />
<TaxonomyField.Categories Terms="" />
<TaxonomyField.Tags Terms="" />
<BodyPart Text="MAIN CONTENT OF THE BLOG POST"
/>
<CommonPart Owner="/User.UserName=Owain" Container="/alias=blog" CreatedUtc="2012-09-10T13:27:00Z" PublishedUtc="2012-09-25T08:57:25Z" ModifiedUtc="2012-09-25T08:56:15Z" />
<AutoroutePart Alias="The Blog/2012/09/10/on-starters-orders" UseCustomPattern="false" />
<TitlePart Title="On starters orders....." />
<CommentsPart CommentsShown="true" CommentsActive="true" ThreadedComments="false" />
<TagsPart Tags="" />
</BlogPost>
What I need to convert it to is:
<posts>
<post id="1" date-created="2012-09-25T08:57:25Z" date-modified="2012-09-25T08:56:15Z" approved="true" post-url="on-starters-orders" type="normal" hasexcerpt="false" views="0" is-published="True">
<title type="text"><![CDATA[On starters orders.....]]></title>
<content type="text"><![CDATA[MAIN CONTENT OF THE BLOG POST]]>
</content>
<post-name type="text"><![CDATA[On starters orders.....]]></post-name>
<categories>
<category ref="1018" />
</categories>
<tags>
<tag ref="training" />
</tags>
<comments>
<comment id="35" date-created="2006-09-05T11:36:50" date-modified="2006-09-05T11:36:50" approved="false" user-name="Phil Haack" user-url="http://haacked.com">
<title type="text"><![CDATA[re: CS Dev Guide: Send Emails]]></title>
<content type="text"><![CDATA[Another test comment.]]></content>
</comment>
</comments>
<authors>
<author ref="Owain" />
</authors>
</post>
Looking for suggestion on the best way to do this as I have 150+ posts to convert and don't fancy doing it manually.
You can use XSLT transformation which has a lot of options to do what you are looking for. XSLT can output in xml.