Generating class file from xsd file using vs command prompt issue - c#

I have this following xsd file. I try to generate class using vs command line like:
command line word = xsd test.xsd /c
"Data at the root level is invalid. Line2, position1.". I am not sure why I having this error. Please advise.
<?xml version="1.0" ?>
- <ResultSet>
- <DataRow>
<Mailing>CCACCL0030</Mailing>
<MailingDesc>'other response' chelsea CA</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
- <DataRow>
<Mailing>VOUCHER20</Mailing>
<MailingDesc>£20 T&M Voucher</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
-
</ResultSet>

To generate code:
Edit the example file so that the & character is encoded. Specifically, swap it for &
Rename the file to test.xml
Run xsd.exe test.xml to generate a schema e.g. test.xsd
Run xsd.exe /c test.xsd to generate classes from the schema
EDIT: This worked fine for me. Here's the contents of my test.xml:
<?xml version="1.0" ?>
<ResultSet>
<DataRow>
<Mailing>CCACCL0030</Mailing>
<MailingDesc>'other response' chelsea CA</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
<DataRow>
<Mailing>VOUCHER20</Mailing>
<MailingDesc>£20 T&M Voucher</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
</ResultSet>

Related

doc.LoadXml giving error data at the root level is invalid

I am having xml response like below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CreateMeetMeOrWebMeetingResults
xmlns:ns2="coss-ws-common">
<transactionId>z8kflt5fi2x28vd51</transactionId>
<status>OK</status>
<meetme id="178635601">
<billingCode>MR5942965</billingCode>
<confirmationId>Q-1GFR347H6I</confirmationId>
<serviceId>1021</serviceId>
<serviceName>BT MeetMe</serviceName>
<timezone>America/New_York</timezone>
<topic>Danny Chung</topic>
<chairpasscode>34132738</chairpasscode>
<participantpasscode>84781442</participantpasscode>
<dialInNumber>
<phoneType>PSTN</phoneType>
<formattedNumber>0203 463 9676</formattedNumber>
<displayName>Direct: 0203 463 9676</displayName>
<rawNumber>02034639676</rawNumber>
</dialInNumber>
<createDate>2014-11-25T09:34:33.168Z</createDate>
<createdByApplicationId>1120</createdByApplicationId>
<bridgeId>892</bridgeId>
<recurringFlag>false</recurringFlag>
<globalAccessFeature locked="false" enabled="true">
<url>Please contact help desk</url>
</globalAccessFeature>
<meetingType>MEETME</meetingType>
<serviceType>MEETME</serviceType>
<pendingBlackoutDateApproval>false</pendingBlackoutDateApproval>
<chairpersonRecord locked="false" enabled="true"/>
</meetme>
</CreateMeetMeOrWebMeetingResults>
I am trying to convert it into xml by doc.LoadXml(response) and its giving error for invalid data at root level.
if somebody can help to solve the issue
If that is your complete xml, you missed a closing tag:
</CreateMeetMeOrWebMeetingResults>
Try adding it at the bottom of your xml
Looking at your indentation, could be that your xml starts with a whitespace. This could be another reason
You don't seem to have a root element for your xml. This works for me.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<CreateMeetMeOrWebMeetingResults xmlns:ns2="coss-ws-common"/>
<transactionId>z8kflt5fi2x28vd51</transactionId>
<status>OK</status>
<meetme id="178635601">
<billingCode>MR5942965</billingCode>
<confirmationId>Q-1GFR347H6I</confirmationId>
<serviceId>1021</serviceId>
<serviceName>BT MeetMe</serviceName>
<timezone>America/New_York</timezone>
<topic>Danny Chung</topic>
<chairpasscode>34132738</chairpasscode>
<participantpasscode>84781442</participantpasscode>
<dialInNumber>
<phoneType>PSTN</phoneType>
<formattedNumber>0203 463 9676</formattedNumber>
<displayName>Direct: 0203 463 9676</displayName>
<rawNumber>02034639676</rawNumber>
</dialInNumber>
<createDate>2014-11-25T09:34:33.168Z</createDate>
<createdByApplicationId>1120</createdByApplicationId>
<bridgeId>892</bridgeId>
<recurringFlag>false</recurringFlag>
<globalAccessFeature locked="false" enabled="true">
<url>Please contact help desk</url>
</globalAccessFeature>
<meetingType>MEETME</meetingType>
<serviceType>MEETME</serviceType>
<pendingBlackoutDateApproval>false</pendingBlackoutDateApproval>
<chairpersonRecord locked="false" enabled="true"/>
</meetme>
</root>

How to use LINQ to get data from an XML file?

I have xml files which look like this:
<?xml version="1.0" encoding="utf-8"?>
<record id="177" restricted="false">
<type>record type</type>
<startdate>2000-10-10</startdate>
<enddate>2014-02-01</enddate>
<titles>
<title xml:lang="en" type="main">Main title</title>
<!-- only one title element with type main -->
<title xml:lang="de" type="official">German title</title>
<!-- can have more titles of type official -->
</titles>
<description>description of the record</description>
<categories>
<category id="122">
<name>category name</name>
<description>category description</description>
</category>
<!-- can have more categories -->
</categories>
<tags>
<tag id="5434">
<name>tag name</name>
<description>tag description</description>
</tag>
<!-- can have more tags -->
</tags>
</record>
How do I select the data from these xml files using LINQ, or should I use something else?
You can load xml into XDocument objects using either the Load() method
for files, or the Parse() method for strings:
var doc = XDocument.Load("your-file.xml");
// OR
var doc = XDocument.Parse(yourXmlString);
Then you can access the data using LINQ:
var titles =
from title in doc.XPathSelectElements("//title")
where title.Attribute("type").Value == "official"
select title.Value;
Was searching for examples of Xmlserializer and found this: How to Deserialize XML document
So why not to try. I did Ctrl+C and Edit -> Paste Special -> Paste XML As Classes in Visual Studio 2013 and... Whoa I got all the classes generated. One condition target framework must be 4.5 and this function is available from Visual Studio 2012+ (as stated in that post)

Extracting App version number from XML with C#

I have an XML file as follows. I would like to extract the Version number from this file.
I tried XML parsing. But, that is for node values only. I am able to get this file as string as follows. var doc = XDocument.Load("WMAppManifest.xml");
<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2012/deployment" AppPlatformVersion="8.0">
<DefaultLanguage xmlns="" code="en-US" />
<App xmlns="" ProductID="{a3f55b1e-c183-4645-9b19-87a41a206978}" Title="sometitle" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal" Author="M-Files author" BitsPerPixel="32" Description="Apache Cordova for Windows Phone" Publisher="CordovaExample" PublisherID="{b93a0d8e-5aa9-4d9b-b232-17e2d852e779}">
<IconPath IsRelative="true" IsResource="false">ApplicationIcon.png</IconPath>
</App>
</Deployment>
You can access the XML declaration node as follows:
XmlDeclaration declaration = doc.ChildNodes
.OfType<XmlDeclaration>()
.FirstOrDefault();
You can then read the value of declaration.Version.
Or, if you are after the 'app' version attribute in the XML document itself, try the following
string version = doc.Descendants("App")
.Single()
.Attribute("Version").Value

Errors using Visual Studio XML Schema Definition Tool (Xsd.exe) Schema Validation Warning

I have an xml file that I am trying to create a class file from using the XSD.exe tool in Visual Studio. Here is the XML File:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Header>
<PayloadManifest xmlns="http://www.starstandards.org/webservices/2005/10/transport">
<manifest contentID="Content0" namespaceURI="?" element="?" relatedID="?" version="?" />
</PayloadManifest>
<wsa:Action>http://www.starstandards.org/webservices/2005/10/transport/operations/ProcessMessageResponse</wsa:Action>
<wsa:MessageID>urn:uuid:b56c3b11-d4a7-4372-9e6a-7498aa2aeb06</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:71089910-41d4-4104-bbd7-2b3ac7cece0f</wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsse:Security>
<wsu:Timestamp wsu:Id="Timestamp-abc8d6ea-d75d-42a2-b042-87309692c2bb">
<wsu:Created>2012-06-27T19:14:43Z</wsu:Created>
<wsu:Expires>2012-06-27T19:19:43Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soap:Header>
<soap:Body>
<ProcessMessageResponse xmlns="http://www.starstandards.org/webservices/2005/10/transport">
<payload>
<content id="Content0">
<VehicleSaleGroupsTable xmlns="">
<VehicleSaleGroupRecord xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CompanyNumber>ZE7</CompanyNumber>
<FranchiseType>N</FranchiseType>
<FranchiseCode>NC</FranchiseCode>
<FranchiseDescription>NEW CAR</FranchiseDescription>
<Manufacturer />
<UsedSaleAccount />
<UsedInventoryAccount />
</VehicleSaleGroupRecord>
<VehicleSaleGroupRecord xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CompanyNumber>ZE7</CompanyNumber>
<FranchiseType>N</FranchiseType>
<FranchiseCode>NT</FranchiseCode>
<FranchiseDescription>NEW TRUCK</FranchiseDescription>
<Manufacturer />
<UsedSaleAccount />
<UsedInventoryAccount />
</VehicleSaleGroupRecord>
<VehicleSaleGroupRecord xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CompanyNumber>ZE7</CompanyNumber>
<FranchiseType>U</FranchiseType>
<FranchiseCode>UC</FranchiseCode>
<FranchiseDescription>USED CAR</FranchiseDescription>
<Manufacturer />
<UsedSaleAccount>USALE</UsedSaleAccount>
<UsedInventoryAccount>UINV</UsedInventoryAccount>
</VehicleSaleGroupRecord>
<VehicleSaleGroupRecord xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CompanyNumber>ZE7</CompanyNumber>
<FranchiseType>U</FranchiseType>
<FranchiseCode>UT</FranchiseCode>
<FranchiseDescription>USED TRUCK</FranchiseDescription>
<Manufacturer />
<UsedSaleAccount>USALE</UsedSaleAccount>
<UsedInventoryAccount>UINV</UsedInventoryAccount>
</VehicleSaleGroupRecord>
</VehicleSaleGroupsTable>
</content>
</payload>
</ProcessMessageResponse>
</soap:Body>
</soap:Envelope>
I opened the Visual Studio command line program and typed the following:
C:\xml2xsd\xsd myXML.xml
It generated 5 xsd files and then I typed the following:
C:\xml2xsd\xsd /c myXML.xsd
and I got the following message with no class files created:
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The 'http://www.starstandards.org/webservices/2005/10/transport:ProcessMessageResponse' element is not declared. Line 25, position 16.
Schema validation warning: The 'http://schemas.xmlsoap.org/ws/2004/08/addressing:Action' element is not declared. Line 13, position 16.
Schema validation warning: The 'http://schemas.xmlsoap.org/ws/2004/08/addressing:MessageID' element is not declared. Line 14, position 16.
Schema validation warning: The 'http://schemas.xmlsoap.org/ws/2004/08/addressing:RelatesTo' element is not declared. Line 15, position 16.
Schema validation warning: The 'http://schemas.xmlsoap.org/ws/2004/08/addressing:To' element is not declared. Line 16, position 16.
Schema validation warning: The 'http://www.starstandards.org/webservices/2005/10/transport:PayloadManifest' element is not declared. Line 17, position
16.
Schema validation warning: The 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd:Security' element is not declared. L
ine 18, position 16.
Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.
Error: Error generating classes for schema 'myxmlRESfile'.
- The element 'http://schemas.xmlsoap.org/ws/2004/08/addressing:Action' is missing.
If you would like more help, please type "xsd /?".
Any help would be appreciated in getting these class files generated. I have never used this tool before.
THanks!

How to get the list of scheduled tasks using c#?

I need to get the list of scheduled tasks that are present in the local computer.
How can I achieve that using c# for Windows XP?
1 alternative...:
1) you can read from the folder where task are saved ( Ive just tested it - win7)
c:\Windows\System32\Tasks\
this is simple xml file like this :
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2012-04-05T13:28:54.2106589</Date>
<Author>DAVIDDOM\RoyiN</Author>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>2012-04-05T13:28:43.2046589</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<RunLevel>LeastPrivilege</RunLevel>
<UserId>DAVIDDOM\RoyiN</UserId>
<LogonType>InteractiveToken</LogonType>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<Duration>PT10M</Duration>
<WaitTimeout>PT1H</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\system32\charmap.exe</Command>
</Exec>
</Actions>
</Task>

Categories

Resources