Reputation: 16272
<?xml version="1.0" encoding="windows-1252"?>
<OpenShipments xmlns="x-schema:C:\UPSLabel\OpenShipments.xdr">
<OpenShipment ShipmentOption="" ProcessStatus="">
<ShipTo>
<CompanyOrName>DARMOT Sp. z o.o</CompanyOrName>
<Attention>DARMOT Sp. z o.o</Attention>
<Address1>Ojca Damiana Tynieckiego 46</Address1>
<Address2></Address2>
<Address3>DarÂ3owo</Address3>
<CountryTerritory>PL</CountryTerritory>
<PostalCode>76-150</PostalCode>
<CityOrTown>DarÂ3owo</CityOrTown>
<StateProvinceCounty></StateProvinceCounty>
<Telephone>943143185</Telephone>
</ShipTo>
<ShipmentInformation>
<ServiceType>UPS Standard</ServiceType>
<NumberOfPackages>1</NumberOfPackages>
<DescriptionOfGoods>Remanufactured auto parts</DescriptionOfGoods>
<BillingOption>PP</BillingOption>
</ShipmentInformation>
<Package>
<PackageType>CP</PackageType>
<Weight>1</Weight>
<Reference1>OUR:AWP0021</Reference1>
<Reference2>Job # 41149</Reference2>
<DeclaredValue>
<Amount>999</Amount>
</DeclaredValue>
</Package>
</OpenShipment>
</OpenShipments>
which i need to generate from my class through xml serialization in c#. so please guide me how to write class structure for getting the above xml.
if one look closely my xml then there are few tag with attribute. here it is...
<OpenShipments xmlns="x-schema:C:\UPSLabel\OpenShipments.xdr">
so how write the property which will have attribute like above one ShipmentOption="" ProcessStatus="" and also please tell me how to generate xmlns like xmlns="x-schema:C:\UPSLabel\OpenShipments.xdr" with OpenShipments tag. here i have no knowledge how to handle this situation and the path in xml is not fixed...C:\UPSLabel\OpenShipments.xdr. it will be different based on condition. so please guide me how to write class for the above xml in detail. thanks
Upvotes: 1
Views: 760
Reputation: 1038710
Open up a Visual Studio command prompt. Then use the xsd.exe tool to do the job for you:
C:\work>xsd.exe test.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\work\test.xsd'.
C:\work>xsd.exe /classes test.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\work\test.cs'.
where test.xml
is the file you have shown in your post. As you can see this generates test.cs
which will contain the class you could use to deserialize this XML to:
using (var reader = XmlReader.Create("test.xml"))
{
var serializer = new XmlSerializer(typeof(OpenShipments));
var openShipments = (OpenShipments)serializer.Deserialize(reader);
// TODO: do something with those shipments like for example shipping them :-)
}
Upvotes: 1
Reputation: 3298
You can use XElement class (System.Xml.Linq). Example:
XElement element = new XElement("OpenShipments");
XAttribute attribute = new XAttribute("xmlns", @"x-schema:C:\UPSLabel\OpenShipments.xdr");
element.Add(attribute);
If the path is not fixed you can do something like this:
string path = "C:\..."; // get your path here
XAttribute attribute = new XAttribute("xmlns", @"x-schema:" + path);
Upvotes: 0