I am not trying to save dataset or datatable to a xml file rather create a xml structure for saving related data? For example i would like to know how below data could be in a xml file
User
UserId
Username
Password
Roles
RoleId
UserId [FK]
CreatedOn
will it look like this
<User userid="" username="" password="">
<Roles id="">
<Name></Name>
<Description></Description>
</Roles>
</User>
which structure would be best to use xml files as DB
I think you want to consider implementing the XML in a more normalized manner, similar to how you would do so with a relational database. For instance in your current solution you would be required to type out the entire role structure within every user such as
<User userid="1" username="user01" password="password">
<Roles id="1">
<Name>Role 1</Name>
<Description>This is Role 1</Description>
</Roles>
</User>
<User userid="2" username="user02" password="password">
<Roles id="1">
<Name>Role 1</Name>
<Description>This is Role 1</Description>
</Roles>
</User>
A normalized structure could look like the following
<Roles>
<Role id="1">
<Name>Role 1</Name>
<Description>This is Role 1</Description>
</Role>
</Roles>
<User id="1" username="user01" password="password">
<Roles>
<Role>1</Role>
</Roles>
</User>
<User id="2" username="user02" password="password">
<Roles>
<Role>1</Role>
</Roles>
</User>
Hope this helps
Related
Not sure if anyone has had this issue with UPS's xml api request sample but I am trying to post an xml request to receive UPS rates. I am using their request example which I have posted below. However, this is not a valid xml document because it has more than one root tag, but if I post this to UPS it goes through fine.
What to do about this? Do I need to do some C# hacking to make this work? I am at a loss.
Why would UPS give an invalid example to use for their api? Any help would be great. Thanks.
<?xml version="1.0"?> <AccessRequest xml:lang="en-US"> <AccessLicenseNumber>Your Access License</AccessLicenseNumber> <UserId>Your User Id</UserId> <Password>Your Password</Password> </AccessRequest> <?xml version="1.0"?> <RatingServiceSelectionRequest xml:lang="en-US"> <Request> <TransactionReference> <CustomerContext>Your Customer Context</CustomerContext> </TransactionReference> <RequestAction>Rate</RequestAction> <RequestOption>Rate</RequestOption> </Request> <Shipment> <RateInformation> <NegotiatedRatesIndicator/> </RateInformation> <Shipper> <Name>Shipper Name</Name> <AttentionName>Shipper Attention Name</AttentionName> <PhoneNumber>1234567890</PhoneNumber> <FaxNumber>1234567890</FaxNumber> <ShipperNumber>Your Shipper Number</ShipperNumber> <Address> <AddressLine1>Address Line 1</AddressLine1> <City>City</City> <StateProvinceCode>StateProvinceCode</StateProvinceCode> <PostalCode>PostalCode</PostalCode> <CountryCode>CountryCode</CountryCode> </Address> </Shipper> <ShipTo> <CompanyName>Ship To Company Name</CompanyName> <AttentionName>Ship To Attention Name</AttentionName> <PhoneNumber>1234567890</PhoneNumber> <FaxNumber>1234567890</FaxNumber> <Address> <AddressLine1>Address Line 1</AddressLine1> <City>City</City> <StateProvinceCode>StateProvinceCode</StateProvinceCode> <PostalCode>PostalCode</PostalCode> <CountryCode>CountryCode</CountryCode> </Address> </ShipTo> <ShipFrom> <CompanyName>Ship From Company Name</CompanyName> <AttentionName>Ship From Attention Name</AttentionName> <PhoneNumber>1234567890</PhoneNumber> <FaxNumber>1234567890</FaxNumber> <Address><AddressLine1>Address Line 1</AddressLine1> <City>City</City> <StateProvinceCode>StateProvinceCode</StateProvinceCode> <PostalCode>PostalCode</PostalCode> <CountryCode>CountryCode</CountryCode> </Address> </ShipFrom> <Service> <Code>59</Code> <Description>2nd Day Air AM</Description> </Service> <Package> <PackagingType> <Code>02</Code> <Description>UPS Package</Description> </PackagingType> <PackageWeight> <UnitOfMeasurement> <Code>LBS</Code> </UnitOfMeasurement> <Weight>5</Weight> </PackageWeight> <Dimensions> <Length>10</Length> <Width>5</Width> <Height>7</Height> </Dimensions> </Package> </Shipment> </RatingServiceSelectionRequest>
XML File 1
<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
<AccessLicenseNumber>Your Access License</AccessLicenseNumber>
<UserId>Your User Id</UserId>
<Password>Your Password</Password>
</AccessRequest>
Xml File 2
<?xml version="1.0"?>
<RatingServiceSelectionRequest xml:lang="en-US">
<Request>
<TransactionReference>
<CustomerContext>Your Customer Context</CustomerContext>
</TransactionReference>
<RequestAction>Rate</RequestAction>
<RequestOption>Rate</RequestOption>
</Request>
<Shipment>
<RateInformation>
<NegotiatedRatesIndicator/>
</RateInformation>
<Shipper>
<Name>Shipper Name</Name>
<AttentionName>Shipper Attention Name</AttentionName>
<PhoneNumber>1234567890</PhoneNumber>
<FaxNumber>1234567890</FaxNumber>
<ShipperNumber>Your Shipper Number</ShipperNumber>
<Address>
<AddressLine1>Address Line 1</AddressLine1>
<City>City</City>
<StateProvinceCode>StateProvinceCode</StateProvinceCode>
<PostalCode>PostalCode</PostalCode>
<CountryCode>CountryCode</CountryCode>
</Address>
</Shipper>
<ShipTo>
<CompanyName>Ship To Company Name</CompanyName>
<AttentionName>Ship To Attention Name</AttentionName>
<PhoneNumber>1234567890</PhoneNumber>
<FaxNumber>1234567890</FaxNumber>
<Address>
<AddressLine1>Address Line 1</AddressLine1>
<City>City</City>
<StateProvinceCode>StateProvinceCode</StateProvinceCode>
<PostalCode>PostalCode</PostalCode>
<CountryCode>CountryCode</CountryCode>
</Address>
</ShipTo>
<ShipFrom>
<CompanyName>Ship From Company Name</CompanyName>
<AttentionName>Ship From Attention Name</AttentionName>
<PhoneNumber>1234567890</PhoneNumber>
<FaxNumber>1234567890</FaxNumber>
<Address>
<AddressLine1>Address Line 1</AddressLine1>
<City>City</City>
<StateProvinceCode>StateProvinceCode</StateProvinceCode>
<PostalCode>PostalCode</PostalCode>
<CountryCode>CountryCode</CountryCode>
</Address>
</ShipFrom>
<Service>
<Code>59</Code>
<Description>2nd Day Air AM</Description>
</Service>
<Package>
<PackagingType>
<Code>02</Code>
<Description>UPS Package</Description>
</PackagingType>
<PackageWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>5</Weight>
</PackageWeight>
<Dimensions>
<Length>10</Length>
<Width>5</Width>
<Height>7</Height>
</Dimensions>
</Package>
</Shipment>
</RatingServiceSelectionRequest>
I have a config file like below,
<Hello>
<Maps>
<Map name="X1">
<Roles>
<Role name="Y1" />
</Roles>
</Map>
<Map name="X2">
<Roles>
<Role name="Y2" />
</Roles>
</Map>
</Maps>
</Hello>
Now I want to loop through all the "Map" (X1, X2), but below line of code giving me only one map X2, how to get both,
var a = ConfigurationManager.GetSection("Hello");
This link may help:
XML reading child nodes
You need to have a for-each loop as one of the answers in the link.
I'm using Fedex's WSDL in C# to generate COD shipping labels. On Fedex shipping labels there is an "Invoice #" string on both the shipping label and the COD return label. I want to set my orderid in the request to Fedex such that my orderid shows up as the Invoice #.
It's not obvious to me how to set the Invoice # in Fedex's wsdl request. Has anybody done this?
The way in which you place the order id or invoice number in the labels is following:
Set the invoice number in the package customer reference.
Specify on the COD details node (on the reference indicator) that you want the COD label to include the invoice number as one of its reference.
Please, note that you can also include other references than invoice number (e.g.: PO, customer reference, and tracking).
Here is a sample SOAP envelope for the request depicting what I said before:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ProcessShipmentRequest xmlns="http://fedex.com/ws/ship/v12">
<WebAuthenticationDetail>
<CspCredential>
<Key>CSP_KEY(IF YOU ARE ONE)</Key>
<Password>CIS_PASSWORD(IF YOU ARE ONE)</Password>
</CspCredential>
<UserCredential>
<Key>CSP_USER_KEY(IF YOU BELONG TO THE CSP)</Key>
<Password>CSP_PASSWORD(IF YOU BELONG TO THE CSP)</Password>
</UserCredential>
</WebAuthenticationDetail>
<ClientDetail>
<AccountNumber>ACCOUNT_NUMBER</AccountNumber>
<MeterNumber>METER_NUMBER</MeterNumber>
<ClientProductId>CLIENT_PRODUCT_ID(IF ONE IS PROVIDED)</ClientProductId>
<ClientProductVersion>CLIENT_VERSION(IF ONE IS PROVIDED)</ClientProductVersion>
</ClientDetail>
<TransactionDetail>
<CustomerTransactionId>261</CustomerTransactionId>
</TransactionDetail>
<Version>
<ServiceId>ship</ServiceId>
<Major>12</Major>
<Intermediate>0</Intermediate>
<Minor>0</Minor>
</Version>
<RequestedShipment>
<ShipTimestamp>2013-08-21T14:00:00-04:00</ShipTimestamp>
<DropoffType>REGULAR_PICKUP</DropoffType>
<ServiceType>PRIORITY_OVERNIGHT</ServiceType>
<PackagingType>YOUR_PACKAGING</PackagingType>
<Shipper>
<AccountNumber>ACCOUNT_NUMBER</AccountNumber>
<Contact>
<PersonName>323199 323199</PersonName>
<CompanyName>CSP Testing</CompanyName>
<PhoneNumber>9012633035</PhoneNumber>
<EMailAddress>csp#fedex.com</EMailAddress>
</Contact>
<Address>
<StreetLines>99 Fedex parkway</StreetLines>
<City>ALAMEDA</City>
<StateOrProvinceCode>CA</StateOrProvinceCode>
<PostalCode>94501</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</Shipper>
<Recipient>
<Contact>
<PersonName>323257 323257</PersonName>
<CompanyName>CSP Testing</CompanyName>
<PhoneNumber>9012633035</PhoneNumber>
<EMailAddress>csp#fedex.com</EMailAddress>
</Contact>
<Address>
<StreetLines>124 Fedex parkway</StreetLines>
<City>PADUCAH</City>
<StateOrProvinceCode>KY</StateOrProvinceCode>
<PostalCode>42001</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</Recipient>
<Origin>
<Contact>
<PersonName>323199 323199</PersonName>
<CompanyName>CSP Testing</CompanyName>
<PhoneNumber>9012633035</PhoneNumber>
<EMailAddress>csp#fedex.com</EMailAddress>
</Contact>
<Address>
<StreetLines>99 Fedex parkway</StreetLines>
<City>ALAMEDA</City>
<StateOrProvinceCode>CA</StateOrProvinceCode>
<PostalCode>94501</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</Origin>
<ShippingChargesPayment>
<PaymentType>SENDER</PaymentType>
<Payor>
<ResponsibleParty>
<AccountNumber>ACCOUNT_NUMBER</AccountNumber>
<Contact>
<PersonName>CSP Testing</PersonName>
<CompanyName>RTC Testing</CompanyName>
</Contact>
<Address>
<CountryCode>US</CountryCode>
</Address>
</ResponsibleParty>
</Payor>
</ShippingChargesPayment>
<SpecialServicesRequested>
<SpecialServiceTypes>COD</SpecialServiceTypes>
<CodDetail>
<CodCollectionAmount>
<Currency>USD</Currency>
<Amount>50</Amount>
</CodCollectionAmount>
<CollectionType>ANY</CollectionType>
<ReferenceIndicator>INVOICE</ReferenceIndicator>
</CodDetail>
</SpecialServicesRequested>
<CustomsClearanceDetail>
<DocumentContent>NON_DOCUMENTS</DocumentContent>
</CustomsClearanceDetail>
<LabelSpecification>
<LabelFormatType>COMMON2D</LabelFormatType>
<ImageType>PNG</ImageType>
<LabelStockType>PAPER_4X6</LabelStockType>
</LabelSpecification>
<RateRequestTypes>ACCOUNTACCOUNT</RateRequestTypes>
<PackageCount>1</PackageCount>
<RequestedPackageLineItems>
<SequenceNumber>1</SequenceNumber>
<GroupPackageCount>1</GroupPackageCount>
<InsuredValue>
<Currency>USD</Currency>
<Amount>5000</Amount>
</InsuredValue>
<Weight>
<Units>LB</Units>
<Value>20</Value>
</Weight>
<PhysicalPackaging>OTHER</PhysicalPackaging>
<ItemDescription>MacBook Pro</ItemDescription>
<CustomerReferences>
<CustomerReferenceType>INVOICE_NUMBER</CustomerReferenceType>
<Value>INVOICE12345</Value>
</CustomerReferences>
<SpecialServicesRequested>
<SpecialServiceTypes>SIGNATURE_OPTION</SpecialServiceTypes>
<SignatureOptionDetail>
<OptionType>SERVICE_DEFAULT</OptionType>
</SignatureOptionDetail>
</SpecialServicesRequested>
</RequestedPackageLineItems>
</RequestedShipment>
</ProcessShipmentRequest>
</soap:Body>
</soap:Envelope>
This is the generated shipping label:
This is the generated COD label:
I hope my reply is of help to you.
Best!
I have an XML file that's modeled something like the following:
<data>
<customer>
<id></id>
<name></name>
<model>
<id></id>
<name></name>
<item>
<id></id>
<history>
<date></date>
<location></location>
</history>
</item>
<item>
<id></id>
<history>
<date></date>
<location></location>
</history>
</item>
</model>
<model>
<id></id>
<name></name>
<item>
<id></id>
<history>
<date></date>
<location></location>
</history>
</item>
</model>
</customer>
<customer>
<id></id>
<name></name>
<model>
<id></id>
<name></name>
<item>
<id></id>
<history>
<date></date>
<location></location>
</history>
</item>
<item>
<id></id>
<history>
<date></date>
<location></location>
</history>
</item>
</model>
</customer>
<customer>
<id></id>
<name></name>
</customer>
</data>
Using XPath in C#, I need to access the following for each customer:
customer/id
customer/name
customer/model/id
customer/model/name
customer/model/item/id
customer/model/item/history/date
customer/model/item/history/location
When data does not exist for any given customer, then the result stored will be null, since all fields of my customer object must be populated. If the XML file was uniform, this would be easy. My problem is accessing each customer's data when each customer may potentially have a different number of model and item nodes. Any ideas?
Assuming that the result will consist of these types of object:
Customer, Model, Item and History
The pseudo - code for populating them is:
Select all /data/customer elements
For each of the nodes selected in 1. do:
Select ./id and ./name and populate the corresponding properties of the object.
Populate a List<Model> property from all model children of the current customer element:
Select all ./model children of the current element.
For each selected model element in 5. create a Model object and populate its properties:
For the current Model object populate its Id and Name properties by selecting the ./id and ./name children of the current model element.
Populate a List<Item> property from all item children of the current model element:
Select all ./item children of the current element.
For the current Item object populate its Id property by selecting the ./id child of the current item element.
In a similar way populate the History property of the Item object with a History object, that you create and populate from the ./history child of the current item element.
Of course, you can skip all this if you use XML Serialization -- read about this here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Coding Platform: ASP.NET C#
I have an XML like this.
<Items>
<Map id="35">
<Terrains>
<Item id="1" row="0" column="0"/>
<Item id="1" row="0" column="1"/>
<Item id="1" row="0" column="2"/>
<Item id="1" row="0" column="3"/>
<Item id="1" row="0" column="4"/>
</Terrains>
</Map>
</Items>
I would like to minify this to
<Its>
<Map id="30">
<Te>
<It id="1" r="0" c="0"/>
<It id="1" r="0" c="1"/>
<It id="1" r="0" c="2"/>
<It id="1" r="0" c="3"/>
<It id="1" r="0" c="4"/>
</Te>
</Map>
</Its>
Then I am converting this to JSON using James Newton-King's JSON Converter.
The idea is to minify the xml data to the maximum as it contains tens of thousands of lines.
My questions are
What is the optimal method to minify the xml as mentioned above?
Now its done like XML-MinifyXML-Convert to JSON. Can I do it in two steps?(XML-Minify while converting to JSON)
Is James Newton-King's JSON converter a bit overkill for this simple conversion?
Please provide code snippets also if possible.
I suspect GZIP (via GZipStream, or simply via IIS, noting that you need to enable dynamic compression for the json mime-type) would be both simpler and smaller, but if you are using serializarion, simply adding some [XmlElement(...)] / [XmlAttribute(...)] should do it. Of course, if size is your concern, can I also suggest something like protobuf-net, which gives an extremely dense binary output.
If you aren't using serialisation, then this looks an ideal fit for some "xslt":
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy><xsl:apply-templates select="#* | node()"/></xsl:copy>
</xsl:template>
<xsl:template match="/Items">
<Its><xsl:apply-templates/></Its>
</xsl:template>
<xsl:template match="/Items/Map/Terrains">
<Te><xsl:apply-templates/></Te>
</xsl:template>
<xsl:template match="/Items/Map/Terrains/Item">
<It id="{#id}" r="{#row}" c="{#column}"><xsl:apply-templates select="*"/></It>
</xsl:template>
</xsl:stylesheet>
(with C#:)
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("Condense.xslt"); // cache and re-use this object; don't Load each time
xslt.Transform("Data.xml", "Smaller.xml");
Console.WriteLine("{0} vs {1}",
new FileInfo("Data.xml").Length,
new FileInfo("Smaller.xml").Length);