Deserialize XML up to a CERTAIN point - c#

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>

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

Want to extract tags from XElements and display as grid on ASP.NET Webform

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
}

Quartz.net Configure several jobs in XML

How to configure correctly the XML for several jobs in Quartz.Net? Is there any tutorials on that?
I found tutorials for one job in the XML. It is working. But I am not sure for several jobs how to configure the quartz_jobs.xml.
I agree with you that a "jobs" (plural) xml-tag would be more intuitive. But basically you put in multiple "job" (singular) xml elements. And just ignore the fact there is no <jobs> (plural) element.
Note that the
<job><name>
needs to match the
<trigger><simple><job-name>
value, to get the job to map to the correct trigger.
But here is my xml for 2 jobs.
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<!-- This value wipes out existing jobs...be very careful with it being "true" -->
<processing-directives>
<overwrite-existing-data>true</overwrite-existing-data>
</processing-directives>
<schedule>
<!-- First Job -->
<job>
<name>LogStuffJobJobName</name>
<group>LogStuffJobGroupName</group>
<description>LogStuffJobNonConcurrentJob Description</description>
<job-type>MyCompany.Apps.QuartzPOC.BAL.Jobs.LogStuffJobNonConcurrentJob, MyCompany.Apps.QuartzPOC.BAL</job-type>
<durable>true</durable>
<recover>false</recover>
<job-data-map>
<!--Parameters for the job-->
<entry>
<key>JobDetailParameter001</key>
<value>Value001_Set_In_Quartz_Jobs_001_xml</value>
</entry>
<entry>
<key>DefinedInJobDetailAndTriggerKey</key>
<value>DefinedInJobDetailAndTriggerKeyValue_JobElement_Set_In_Quartz_Jobs_001_xml</value>
</entry>
</job-data-map>
</job>
<trigger>
<simple>
<name>LogStuffJobTriggerName</name>
<group>LogStuffJobTriggerGroup</group>
<description>LogStuffJobTriggerName Description</description>
<job-name>LogStuffJobJobName</job-name>
<job-group>LogStuffJobGroupName</job-group>
<job-data-map>
<entry>
<key>TriggerParameter001Key</key>
<value>TriggerParameter001Value_Set_In_Quartz_Jobs_001_xml</value>
</entry>
<entry>
<key>TriggerParameter002Key</key>
<value>TriggerParameter002Value_Set_In_Quartz_Jobs_001_xml</value>
</entry>
<entry>
<key>DefinedInJobDetailAndTriggerKey</key>
<value>DefinedInJobDetailAndTriggerKeyValue_TriggerElement_Set_In_Quartz_Jobs_001_xml</value>
</entry>
</job-data-map>
<!--<start-time>1982-06-28T18:15:00.0Z</start-time>-->
<!--<end-time>2020-05-04T18:13:51.0Z</end-time>-->
<misfire-instruction>SmartPolicy</misfire-instruction>
<!-- repeat indefinitely every 5 seconds -->
<repeat-count>-1</repeat-count>
<repeat-interval>5000</repeat-interval>
</simple>
</trigger>
<!-- Second Job -->
<job>
<name>TextFilePoppingJobJobName</name>
<group>TextFilePoppingJobGroupName</group>
<description>TextFilePoppingJob Desription</description>
<job-type>MyCompany.Apps.QuartzPOC.BAL.Jobs.TextFilePoppingJob, MyCompany.Apps.QuartzPOC.BAL</job-type>
<durable>true</durable>
<recover>false</recover>
<job-data-map>
<entry>
<key>FavoriteActor</key>
<value>Harrison Ford</value>
</entry>
<entry>
<key>FavoriteColor</key>
<value>Green</value>
</entry>
</job-data-map>
</job>
<trigger>
<simple>
<name>TextFilePoppingJobTriggerName</name>
<group>TextFilePoppingJobTriggerGroup</group>
<description>TextFilePoppingJobTriggerName Description</description>
<job-name>TextFilePoppingJobJobName</job-name>
<job-group>TextFilePoppingJobGroupName</job-group>
<job-data-map>
<entry>
<key>FavoriteCity</key>
<value>Chicago</value>
</entry>
</job-data-map>
<misfire-instruction>SmartPolicy</misfire-instruction>
<repeat-count>-1</repeat-count>
<repeat-interval>10000</repeat-interval>
</simple>
</trigger>
</schedule>
</job-scheduling-data>

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.

How to insert a child element into an existing element

I am using a XDocument to write an xml file and I am writing to the document in two different places. After the first write I have
<?xml version="1.0" encoding="utf-8"?>
<suspensedata connectionid="000" customerid="000" name="MyName" />
After the second write I want the file to look like this
<?xml version="1.0" encoding="utf-8"?>
<suspensedata connectionid="560" customerid="131" name="ImgTransfer2327">
<transaction DocumentID="46" SuspenseID="7">
<field id="LocationID">000000015000</field>
<field id="AccountNumber">50000</field>
<field id="AmountPaid">25.00</field>
<field id="CheckAmount">100.00</field>
<field id="CheckNo">000</field>
</transaction>
</suspensedata>
But I can't seem to get the insert done correctly.
I've tried (The name of my XDocument is ValidXml) ValidXml.Root.Add(new Element("transaction"));
and that does not change anything.
I have also tried ValidXml.Element("suspensedata").Add(new XElement("transaction"));
But that did not work either.
How would I add this child element?
EDIT: Both attempts did not produce any other output besides the output on the first try. Also I did make sure to use ValidXml.Save()
I have tried something like this I supposed that the two parts are in 2 files
//xmlfile1 contains the first part
<?xml version="1.0" encoding="utf-8"?>
<suspensedata connectionid="000" customerid="000" name="MyName" />
//this part will be loaded like this
XDocument xDoc = XDocument.Load("xmlfile1.xml");
XElement elt = xDoc.Root;
//and the second file contains the second part
XDocument xDoc2 = XDocument.Load("xmlfile2.xml");
XElement elt2 = xDoc2.Root;
elt.Add(elt2);
xDoc.Save("xmlfile1.xml");
Hope this help

Categories