I'm attempting to convert JSON sent from a web browser client to the server so I can send the data as parameters in the form of XML to an SQL database. I'm struggling to do this as I have objects inside of objects and I'm not sure how to convert them dynamically into a structured XML format. Below are examples of the JSON I am using, the possible XML formats I'm trying to turn it into (or anything close to them), and the code that I am using.
JSON:
[
{"value":50,"name":"desired_gross_margin","type":"int"},
{"value":50,"name":"desired_adjusted_gross_margin","type":"int"},
{"value":0,"name":"target_electricity_tariff_unit_charge","type":"decimal"},
{"value":0,"name":"target_electricity_tariff_standing_charge","type":"decimal"},
{"value":0,"name":"target_gas_tariff_unit_charge","type":"decimal"},
{"value":0,"name":"target_gas_tariff_standing_charge","type":"decimal"},
{"value":"10/10/2016","name":"planned_go_live_date","type":"DateTime"},
{"value":"0","name":"assumed_fuel_ratio","type":"int"},
{"value":{
"year_one":"Cold",
"year_two":"Average",
"year_three":"Warm"
},
"name":"weather_variable","type":"string"}
]
Possible XML outputs:
1:
<Filters>
<CustomerParameters>
<CustomParameter name="desired_gross_margin" type="int" value="50"/>
<CustomParameter name="desired_adjusted_gross_margin" type="int" value="50"/>
<CustomParameter name="target_electricity_tariff_unit_charge" type="decimal" value="0"/>
<CustomParameter name="target_electricity_tariff_standing_charge" type="decimal" value="0"/>
<CustomParameter name="target_gas_tariff_unit_charge" type="decimal" value="0"/>
<CustomParameter name="target_gas_tariff_standing_charge" type="decimal" value="0"/>
<CustomParameter name="planned_go_live_date" type="DateTime" value="10/10/2016"/>
<CustomParameter name="assumed_fuel_ratio" type="int" value="0"/>
<CustomParamaters name="weather_variables">
<CustomParameter name="year_one" type="string" value="Cold"/>
<CustomParameter name="year_two" type="string" value="Average"/>
<CustomParameter name="year_three" type="string" value="Cold"/>
</CustomParameters>
</CustomParameters>
</Filters>
2:
<?xml version="1.0" encoding="UTF-8" ?>
<0>
<value>50</value>
<name>desired_gross_margin</name>
<type>int</type>
</0>
<1>
<value>50</value>
<name>desired_adjusted_gross_margin</name>
<type>int</type>
</1>
<2>
<value>0</value>
<name>target_electricity_tariff_unit_charge</name>
<type>decimal</type>
</2>
<3>
<value>0</value>
<name>target_electricity_tariff_standing_charge</name>
<type>decimal</type>
</3>
<4>
<value>0</value>
<name>target_gas_tariff_unit_charge</name>
<type>decimal</type>
</4>
<5>
<value>0</value>
<name>target_gas_tariff_standing_charge</name>
<type>decimal</type>
</5>
<6>
<value>10/10/2016</value>
<name>planned_go_live_date</name>
<type>DateTime</type>
</6>
<7>
<value>0</value>
<name>assumed_fuel_ratio</name>
<type>int</type>
</7>
<8>
<value>
<year_one>Cold</year_one>
<year_two>Average</year_two>
<year_three>Warm</year_three>
</value>
<name>weather_variable</name>
<type>string</type>
</8>
</xml>
C# code:
ForecastController.cs:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Script.Serialization;
using System.Xml;
namespace ForecastServices.Controllers
{
public class ForecastController : ApiController
{
[HttpPost]
public List<Data> GetData(HttpRequestMessage request)
{
string connection_string = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
string sql = "DataHub.get_GrossMarginModel";
string json = request.Content.ReadAsStringAsync().Result;
//var filters2 = new JavaScriptSerializer().Deserialize<dynamic>(json); //this works but I can't turn it into XML! :(
List<Filter> filters = new JavaScriptSerializer().Deserialize<List<Filter>>(json);
string xml = Filter.getFilterListXML(filters);
List<Data> data = new List<Data>();
using(SqlConnection connection = new SqlConnection(connection_string))
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.Add(new SqlParameter("filter_xml", ""));
cmd.CommandType = CommandType.StoredProcedure;
var adapter = new SqlDataAdapter(cmd);
var set = new DataSet();
cmd.ExecuteNonQuery();
adapter.Fill(set);
if (set.Tables.Count > 0)
{
foreach (DataRow tableRow in set.Tables[0].Rows)
{
data.Add(new Data()
{
name = tableRow.ItemArray[0].ToString(),
year_one = (int)tableRow.ItemArray[1],
year_two = (int)tableRow.ItemArray[2],
year_three = (int)tableRow.ItemArray[3],
});
}
}
connection.Close();
}
return data;
}
}
}
Filter.cs:
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace ForecastServices.Domain
{
public class Filter
{
public string value { get; set; }
public string name { get; set; }
public string type { get; set; }
public string getXML()
{
return string.Format("<CustomParameter name=\"{0}\" type=\"{1}\" value=\"{2}\"/>", name, type, value);
}
public static string getFilterListXML(ICollection<Filter> filters)
{
StringBuilder XMLString = new StringBuilder();
XMLString.Append("<Filters><CustomerParameters>");
foreach (Filter f in filters)
{
XMLString.Append(f.getXML());
}
XMLString.Append("</CustomParameters></Filters>");
return XMLString.ToString();
}
}
}
Not building on your current code, you can take a look at this question
Using this one-liner:
XmlDocument doc = JsonConvert.DeserializeXmlNode("{\"Row\":" + json + "}", "root"); // JSON needs to be an object
You can end up with:
<root>
<Row>
<value>50</value>
<name>desired_gross_margin</name>
<type>int</type>
</Row>
<Row>
<value>50</value>
<name>desired_adjusted_gross_margin</name>
<type>int</type>
</Row>
<Row>
<value>0</value>
<name>target_electricity_tariff_unit_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>0</value>
<name>target_electricity_tariff_standing_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>0</value>
<name>target_gas_tariff_unit_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>0</value>
<name>target_gas_tariff_standing_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>10/10/2016</value>
<name>planned_go_live_date</name>
<type>DateTime</type>
</Row>
<Row>
<value>0</value>
<name>assumed_fuel_ratio</name>
<type>int</type>
</Row>
<Row>
<value>
<year_one>Cold</year_one>
<year_two>Average</year_two>
<year_three>Warm</year_three>
</value>
<name>weather_variable</name>
<type>string</type>
</Row>
</root>
Where json is the input JSON on your question, it's close to what you want (which your post says is sort of okay), but if you want to go for your 1st and 2nd options, you can simply build new XML by manipulating this (renaming the nodes, etc.).
Related
I am trying to edit a existing XML file through C# and need to identify the sections I want (block name="treeDeadTree01" and block name="treeDeadTree02") Then get the value of "count" in the child element "drop". I have been able to identify the elements (block name="treeDeadTree01" and block name="treeDeadTree02") but cant figure out how to get the value of "count" from the element "drop" of the specific elements (block name="treeDeadTree01" and block name="treeDeadTree02")
The XML file is a config file from the game 7 Days To Die and I am trying to make a application to more easily edit the configs of the game. After I find the value of "count" I am putting it into a DataGridView.
My question is, how after I find the block element I want do I find the value of "count" in the "drop" child element of the block element?
This is a section of the XML file I am trying to edit
<blocks>
<block name="treeDeadTree01">
<property name="Extends" value="treeMaster" />
<property name="Model" value="Entities/Trees/White_Oak22Prefab" />
<property name="ModelOffset" value="0,-0.3,0" />
<property name="MultiBlockDim" value="1,4,1" />
<property name="BigDecorationRadius" value="4" />
<property name="Collide" value="movement,melee,bullet,arrow,rocket" />
<drop event="Harvest" name="resourceWood" count="515" tag="oreWoodHarvest" />
<property name="ParticleOnDeath" value="treeGib_dead_01" />
<!-- <drop event="Destroy" name="treePlantedMaple1m" count="x"/> -->
<property name="SortOrder2" value="0140" />
<!-- SortTree -->
</block>
<block name="treeDeadTree02">
<!-- almost a shrub -->
<property name="Extends" value="treeMaster" />
<property name="IsTerrainDecoration" value="true" />
<property name="Model" value="Entities/Trees/Haunted_Shrub_WastelandPrefab" />
<property name="ModelOffset" value="0,-0.3,0" />
<property name="Collide" value="melee,rocket" />
<!-- no movement effect on Shape="ModelTree" -->
<drop event="Harvest" name="resourceWood" count="215" tag="oreWoodHarvest" />
<property name="ParticleOnDeath" value="treeGib_dead_02" />
<!-- <drop event="Destroy" name="treePlantedMaple1m" count="x"/> -->
<property name="FilterTags" value="foutdoor,ftrees,fshrubbery" />
<property name="SortOrder1" value="a060" />
</block>
</blocks
My current code
string[] GatherRateNames = {"treeDeadTree01", "treeDeadTree02"};
string XMLBlocksPath = "F:\\SteamLibrary\\steamapps\\common\\7 Days To Die\\Data\\Config\\blocks.xml";
private void makeGatherRatesList()
{
XmlDocument doc = new XmlDocument();
doc.Load(XMLBlocksPath);
XmlNodeList aNodes = doc.SelectNodes("/blocks/block");
foreach (XmlNode aNode in aNodes)
{
XmlAttribute idAttribute = aNode.Attributes["name"];
if (GatherRateNames.Contains(idAttribute.ToString()))
{
addItemToList(idAttribute.ToString(),"Value of count");
}
}
}
private void addItemToList(string itemName, int itemValue)
{
}
In the end I want to get the name of the block like "treeDeadTree01" then get the value of "count" inside the element "drop" of that block and put both of those in a DataGridView and be able to edit the value of count through the DataGridView then repeat that with every block I need.
Using Xml Linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
CultureInfo culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ",";
culture.NumberFormat.NumberGroupSeparator = ".";
XmlReader reader = XmlReader.Create(FILENAME);
List<Block> blocks = new List<Block>();
while (!reader.EOF)
{
if (reader.Name != "block")
{
reader.ReadToFollowing("block");
}
if (!reader.EOF)
{
Block newBlock = new Block();
blocks.Add(newBlock);
XElement xBlock = (XElement)XElement.ReadFrom(reader);
newBlock.name = (string)xBlock.Attribute("name");
XElement drop = xBlock.Element("drop");
if (drop != null)
{
object count = drop.Attribute("count");
newBlock.count = (count == null)? null : (decimal?)decimal.Parse((string)count, culture);
}
}
}
}
public class Block
{
public string name { get; set; }
public decimal? count { get; set; }
}
}
}
I have this XML file:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPasswordSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PasswordSettings>
<CustomerRef>c</CustomerRef>
<Node>n</Node>
<Name>na</Name>
<Login>l</Login>
<Password>ITra+Map1RxklmcSY5yOo9wU9tUV0S4C4qwUv4p2ZFS3L8ByJYXmA9YjswlSTjQZXUJAkV3Z6mhY8OF5/dFOLNAZZRk2i2IOzrVOWSDfdpB8/Vm7PPF0ucSHILHNWpT8</Password>
<FileType>ft</FileType>
</PasswordSettings>
<PasswordSettings>
<CustomerRef>c</CustomerRef>
<Node>n</Node>
<Name>na</Name>
<Login>l</Login>
<Password>ITra+Map1RxklmcSY5yOo9wU9tUV0S4C4qwUv4p2ZFS3L8ByJYXmA9YjswlSTjQZXUJAkV3Z6mhY8OF5/dFOLNAZZRk2i2IOzrVOWSDfdpB8/Vm7PPF0ucSHILHNWpT8</Password>
<FileType>ft</FileType>
</PasswordSettings>
</ArrayOfPasswordSettings>
As you see there are multiple <PasswordSettings> which is a list of multiple items like name, login and password. Can I iterate the <PasswordSettings> in some foreach <PasswordSettings> loop and get the elements?
Please try with this example :
XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
<Brand name="Brand1">
<product name="Product1" />
<product name="Product2" />
</Brand>
<Brand name="Brand2">
<product name="Product3" />
<product name="Product4" />
</Brand>
</root>
C#:
StringBuilder result = new StringBuilder();
foreach (XElement level1Element in XElement.Load(#"D:\product.xml").Elements("Brand"))
{
result.AppendLine(level1Element.Attribute("name").Value);
foreach (XElement level2Element in level1Element.Elements("product"))
{
result.AppendLine(" " + level2Element.Attribute("name").Value);
}
}
Try xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication108
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("PasswordSettings").Select(x => new
{
c = (string)x.Element("CustomerRef"),
node = (string)x.Element("Node"),
name = (string)x.Element("Name"),
login = (string)x.Element("Login"),
password = (string)x.Element("Password"),
fileType = (string)x.Element("FileType")
}).ToList();
}
}
}
I have an XML file that I need to read and I having trouble moving to a specific element to then start parsing over child elements.
The element that I need to start reading from is <MenuCodes>, what is the best method of navigating to the element and then start reading?
Sample XML file
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="8eb2de20-8dff-4460-ffff-866ac948dfab" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">b921a5f3-8188-4021-9c6f-416bdf78f629</ActivityId>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetMenusResponse xmlns="http://www...">
<GetMenusResult>
<Header xmlns="http://www...">
<SessionId>{8CCCED2E-3D44-4200-BBAC-66660CA08692}</SessionId>
<CorrelationId>{AFFED12-AD36-4A2C-99FE-5E8D7CBC681D}</CorrelationId>
<TimeStamp>2018-09-14T11:36:21.5123749Z</TimeStamp>
</Header>
<ResponseStatus xmlns="http://www...">
<Status>OK</Status>
</ResponseStatus>
<ResponseBody xmlns="http://www...">
<Refreshed>2018-09-14T11:36:22.0115845Z</Refreshed>
<Menus>
<MenuCodes>
<MenuCode Code="AAA/A1" ShortText="Foo">
<ValidPrefix Prefix="V" />
<ValidPrefix Prefix="D" />
<ValidPrefix Prefix="N" />
</MenuCode>
<MenuCode Code="AAA/A2" ShortText="Foo2">
<ValidPrefix Prefix="D" />
<ValidPrefix Prefix="N" />
</MenuCode>
</MenuCodes>
</Menus>
</ResponseBody>
</GetMenusResult>
</GetMenusResponse>
</s:Body>
</s:Envelope>
Here's a really verbose way of getting to that element, the important bit that most newcomers forget here is to add in the namespace:
var doc = XDocument.Parse(xml);
var soapNs = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
var otherNs = XNamespace.Get("http://www...");
var menuCodes = doc
.Element(soapNs + "Envelope")
.Element(soapNs + "Body")
.Element(otherNs + "GetMenusResponse")
.Element(otherNs + "GetMenusResult")
.Element(otherNs + "ResponseBody")
.Element(otherNs + "Menus")
.Element(otherNs + "MenuCodes");
The namespaces are an issue. So don't use namespace. See one line code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication68
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement menuCodes = doc.Descendants().Where(x => x.Name.LocalName == "MenuCodes").FirstOrDefault();
}
}
}
I have a FIX (Financial Information Exchange) message format, e.g. 35=U1 49=GEMI1 8=FIX.4.1 9=732 or 8=FIX.4.1 9=751 35=U1 34=3 49=GEMI2 52=20160125-10:52:21, which I converted to XML format.
<messageTags>
<tag key="9" value="751" />
<tag key="8" value="FIX.4.1" />
<tag key="35" value="U1" />
<tag key="34" value="3" />
<tag key="49" value="GEMI2" />
<tag key="52" value="20160125-10:52:21" />
</messageTags>
I want to convert or display this type of xml in a SOAP format like below.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eu="http://client.ws.emx.co.uk">
<soapenv:Header></soapenv:Header>
<soapenv:Body>
<eu:processXmlMessageRequest>
<sequenceNumber>1</sequenceNumber>
<creationTime>2016-05-28T09:36:22.165</creationTime>
<messageTags>
<tag key="8" value="FIX.4.1" />
<tag key="9" value="751" />
<tag key="35" value="U1" />
<tag key="34" value="3" />
<tag key="49" value="GEMI2" />
<tag key="52" value="20160125-10:52:21" />
</messageTags>
<payloadType>XmlFixMessageTags</payloadType>
</eu:processXmlMessageRequest>
</soapenv:Body>
</soapenv:Envelope>
Can anybody help on this, how to do it in Visual Studio C#.
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XElement messageTags = XElement.Load(FILENAME);
string header =
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eu=\"http://client.ws.emx.co.uk\">" +
"<soapenv:Header></soapenv:Header>" +
"<soapenv:Body>" +
"<eu:processXmlMessageRequest>" +
"</eu:processXmlMessageRequest>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
XElement envelope = XElement.Parse(header);
XElement request = envelope.Descendants().Where(x => x.Name.LocalName == "processXmlMessageRequest").FirstOrDefault();
int sequenceNumber = 1;
DateTime now = DateTime.Now;
request.Add(new object[] {
new XElement("sequenceNumber", sequenceNumber),
new XElement("creationTime", now.ToString("yyyy-MM-ddT" + now.TimeOfDay.ToString())), //>2016-05-28T09:36:22.165</creationTime>
messageTags
});
}
}
}
This is my response structure that i am getting
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetPrice_HotelResponse xmlns="PricingAirAPI">
<GetPrice_HotelResult xmlns:a="PricingHotelAPI" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Error i:nil="true" />
<a:Results xmlns:b="PricingCommonAPI">
<b:HotelSearchResult>
<b:AmountAfterTax>561.9600</b:AmountAfterTax>
<b:AmountBeforeTax>561.9600</b:AmountBeforeTax>
<b:B2B2BHotelPrice xmlns:c="http://schemas.datacontract.org/2004/07/HotelB2B2BPricingEngine">
<c:AgentMarkUp>0</c:AgentMarkUp>
<c:SubAgentMarkUp>0</c:SubAgentMarkUp>
</b:B2B2BHotelPrice>
<b:BookingSource>JacTravel</b:BookingSource>
<b:CityId i:nil="true" />
<b:CityName i:nil="true" />
<b:CountryCode i:nil="true" />
<b:Currency>GBP</b:Currency>
<b:Discount>0.00</b:Discount>
<b:HotelCode>12035</b:HotelCode>
<b:HotelName i:nil="true" />
<b:HotelSupplierResultType>None</b:HotelSupplierResultType>
<b:IsUniversalApiResult>true</b:IsUniversalApiResult>
<b:Price xmlns:c="http://schemas.datacontract.org/2004/07/TekTravel.Hotel.PricingEngine">
<b:AccPriceType>NetFare</b:AccPriceType>
<b:AdditionalTxnFee>0.0</b:AdditionalTxnFee>
<b:AgentCommission>0</b:AgentCommission>
<b:AgentCommissionRate>0</b:AgentCommissionRate>
<b:AgentIncentiveAmt>0.00</b:AgentIncentiveAmt>
<b:AgentIncentiveRate>0.00</b:AgentIncentiveRate>
<b:AgentMarkUp>0</b:AgentMarkUp>
<b:AgentMarkUpAmt>0.0000</b:AgentMarkUpAmt>
<b:AgentMarkUpType>Percentage</b:AgentMarkUpType>
<b:AgentPLB>0</b:AgentPLB>
<b:AgentPLBRate>0</b:AgentPLBRate>
<b:AirlineBaggageCharges>0</b:AirlineBaggageCharges>
<b:AirlineMealCharges>0</b:AirlineMealCharges>
<b:AirlineSeatCharges>0</b:AirlineSeatCharges>
<b:AirlineTransFee>0</b:AirlineTransFee>
<b:CateringCharge>0</b:CateringCharge>
<b:CessTax>0</b:CessTax>
<b:ChargeBU />
<b:CommissionType>RB</b:CommissionType>
<b:ConvenienceCharges>0</b:ConvenienceCharges>
<b:Currency />
<b:CurrencyCode>INR</b:CurrencyCode>
<b:DealId>0</b:DealId>
<b:Discount>0</b:Discount>
<b:EduCessAmount>0</b:EduCessAmount>
<b:EduCessRate>0</b:EduCessRate>
<b:FareBreakdown i:nil="true" />
<b:IsGPEnabled>false</b:IsGPEnabled>
<b:IsOurServiceTaxOnBaseFarePlusYQ>false</b:IsOurServiceTaxOnBaseFarePlusYQ>
<b:IsOurServiceTaxOnComm>false</b:IsOurServiceTaxOnComm>
<b:IsServiceChargeApplicable>true</b:IsServiceChargeApplicable>
<b:IsServiceTaxOnBaseFarePlusYQ>false</b:IsServiceTaxOnBaseFarePlusYQ>
<b:Markup>80</b:Markup>
<b:NetFare>0</b:NetFare>
<b:OtherCharge>0</b:OtherCharge>
<b:OurCommRate>0</b:OurCommRate>
<b:OurCommission>0</b:OurCommission>
<b:OurCommissionType>RB</b:OurCommissionType>
<b:OurEduCessAmt>0</b:OurEduCessAmt>
<b:OurEduCessRate>0</b:OurEduCessRate>
<b:OurIncentiveAmt>0</b:OurIncentiveAmt>
<b:OurIncentiveRate>0</b:OurIncentiveRate>
<b:OurPLB>0</b:OurPLB>
<b:OurPLBRate>0</b:OurPLBRate>
<b:OurPlbType i:nil="true" />
<b:OurServiceTaxAmt>0</b:OurServiceTaxAmt>
<b:OurServiceTaxRate>0</b:OurServiceTaxRate>
<b:OurTdsCommAmt>0</b:OurTdsCommAmt>
<b:OurTdsIncentiveAmt>0.0</b:OurTdsIncentiveAmt>
<b:OurTdsPLBAmt>0</b:OurTdsPLBAmt>
<b:OurTdsRate>0</b:OurTdsRate>
<b:PlbType i:nil="true" />
<b:PrfCurrency i:nil="true" />
<b:PrfROE>0.0</b:PrfROE>
<b:PriceId>0</b:PriceId>
<b:PublishedFare>0</b:PublishedFare>
<b:RateOfExchange>1</b:RateOfExchange>
<b:ResultId>0</b:ResultId>
<b:ReverseHandlingCharge>0</b:ReverseHandlingCharge>
<b:SegmentFee>0</b:SegmentFee>
<b:ServiceTaxRate>0</b:ServiceTaxRate>
<b:SeviceTax>0</b:SeviceTax>
<b:StockType />
<b:SupplierId>0</b:SupplierId>
<b:TDSPLB>0</b:TDSPLB>
<b:Tax>0</b:Tax>
<b:TaxBreakup i:nil="true" xmlns:d="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
<b:TdsCommission>0</b:TdsCommission>
<b:TdsIncentive>0.00</b:TdsIncentive>
<b:TdsRate>0</b:TdsRate>
<b:TotalGP>0</b:TotalGP>
<b:TransactionFee>0</b:TransactionFee>
<b:WLCharge>0</b:WLCharge>
<b:WLPrice i:nil="true" />
<b:WhiteLabelDiscount>0</b:WhiteLabelDiscount>
<b:YQTax>0</b:YQTax>
<c:DynamicPriceAgentComm>0</c:DynamicPriceAgentComm>
<c:DynamicPriceOurComm>0</c:DynamicPriceOurComm>
</b:Price>
<b:RateType>Negotiated</b:RateType>
<b:RoomDetails />
<b:TotalGP>0</b:TotalGP>
</b:HotelSearchResult>
<b:HotelSearchResult>
.....
</b:HotelSearchResult>
</a:Results>
<a:Status>Successful</a:Status>
<a:TraceId>b748e818-5c6a-4e41-94ca-f8552a328b99</a:TraceId>
</GetPrice_HotelResult>
</GetPrice_HotelResponse>
</s:Body>
</s:Envelope>
Now I want to create xml like
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfHotelSearchResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HotelSearchResult>
<ResultIndex>1</ResultIndex>
<HotelCode>AMB3|LON</HotelCode>
<HotelName>Ambassadors Bloomsbury</HotelName>
<HotelCategory />
<StarRating>FourStar</StarRating>
<HotelDescription>***Special Offer Free welcome gift for stays of 4 or more nights and free bottle of water in each room </HotelDescription>
<HotelPromotion />
<HotelPolicy />
<HotelPicture>http://images.gta-travel.com/HH/Images/GB/LON/LON-AMB3-1.jpg</HotelPicture>
<HotelAddress>12 UPPER WOBURN PLACE London WC1 0HX United Kingdom, , United Kingdom, </HotelAddress>
<HotelContactNo />
<Latitude />
<Longitude />
<AmountBeforeTax>0</AmountBeforeTax>
<AmountAfterTax>0</AmountAfterTax>
<AmountBeforeDiscount>0</AmountBeforeDiscount>
<AmountBeforeTaxInSupplierCurr>259.00</AmountBeforeTaxInSupplierCurr>
<AmountAfterTaxInSupplierCurr>259.00</AmountAfterTaxInSupplierCurr>
<AmountBeforeDiscountInSupplierCurr>259.00</AmountBeforeDiscountInSupplierCurr>
<BookingSource>GTA</BookingSource>
<CurrencySupplier>GBP</CurrencySupplier>
<PreferredROE>0</PreferredROE>
<IsBaseCurrencyRequired>false</IsBaseCurrencyRequired>
<Price>
<CommissionType>RB</CommissionType>
<PlbType>RB</PlbType>
<OurPlbType>RB</OurPlbType>
<AirlineTransFee>0</AirlineTransFee>
<TdsCommission>0</TdsCommission>
<TDSPLB>0</TDSPLB>
<PriceId>0</PriceId>
<PublishedFare>0</PublishedFare>
<NetFare>0</NetFare>
<Markup>0</Markup>
<ServiceTax>0</ServiceTax>
<CessTax>0</CessTax>
<OurCommission>0</OurCommission>
<OurPLB>0</OurPLB>
<AgentCommission>0</AgentCommission>
<AgentPLB>0</AgentPLB>
<OtherCharges>0</OtherCharges>
<Tax>0</Tax>
<WhiteLabelDiscount>0</WhiteLabelDiscount>
<TransactionFee>0</TransactionFee>
<Currency>INR</Currency>
<ChargeBU />
<AccPriceType>PublishedFare</AccPriceType>
<RateOfExchange>0</RateOfExchange>
<AdditionalTxnFee>0</AdditionalTxnFee>
<WLCharge>0</WLCharge>
<Discount>0</Discount>
<ReverseHandlingCharge>0</ReverseHandlingCharge>
<YQTax>0</YQTax>
<IsServiceTaxOnBaseFarePlusYQ>false</IsServiceTaxOnBaseFarePlusYQ>
<TdsRate>0</TdsRate>
<AgentMarkUpType>Percentage</AgentMarkUpType>
<AgentMarkUp>0</AgentMarkUp>
<AgentMarkUpAmt>0</AgentMarkUpAmt>
<ConvenienceCharges>0</ConvenienceCharges>
<PrfROE>0</PrfROE>
<ServiceTaxRate>0</ServiceTaxRate>
<EduCessRate>0</EduCessRate>
<EduCessAmount>0</EduCessAmount>
<AgentCommissionRate>0</AgentCommissionRate>
<AgentPLBRate>0</AgentPLBRate>
<AirlineBaggageCharges>0</AirlineBaggageCharges>
<AirlineMealCharges>0</AirlineMealCharges>
<AirlineSeatCharges>0</AirlineSeatCharges>
<OurCommRate>0</OurCommRate>
<OurPLBRate>0</OurPLBRate>
<OurIncentiveRate>0</OurIncentiveRate>
<OurIncentiveAmt>0</OurIncentiveAmt>
<SegmentFee>0</SegmentFee>
<OurCommissionType>RB</OurCommissionType>
<OurServiceTaxRate>0</OurServiceTaxRate>
<OurServiceTaxAmt>0</OurServiceTaxAmt>
<OurEduCessRate>0</OurEduCessRate>
<OurEduCessAmt>0</OurEduCessAmt>
<OurTdsRate>0</OurTdsRate>
<IsServiceChargeApplicable>true</IsServiceChargeApplicable>
<OurTdsCommAmt>0</OurTdsCommAmt>
<OurTdsPLBAmt>0</OurTdsPLBAmt>
<IsGPEnabled>false</IsGPEnabled>
<TotalGP>0</TotalGP>
<CateringCharge>0</CateringCharge>
<AgentIncentiveRate>0.00</AgentIncentiveRate>
<AgentIncentiveAmt>0.00</AgentIncentiveAmt>
<TdsIncentive>0.00</TdsIncentive>
<DealId>0</DealId>
<SupplierId>0</SupplierId>
<StockType />
<IsPaidByAgentCreditCard>false</IsPaidByAgentCreditCard>
<AmountPaidByAgentCreditCard>0.00</AmountPaidByAgentCreditCard>
<OurTdsIncentiveAmt>0.00</OurTdsIncentiveAmt>
<IsOurServiceTaxOnComm>false</IsOurServiceTaxOnComm>
<IsOurServiceTaxOnBaseFarePlusYQ>false</IsOurServiceTaxOnBaseFarePlusYQ>
<ServiceFee>0</ServiceFee>
<TotalTxnFee>0</TotalTxnFee>
<SwachhBharatCessRate>0</SwachhBharatCessRate>
<SwachhBharatCessAmount>0</SwachhBharatCessAmount>
</Price>
<RateType>Negotiated</RateType>
<CityName />
<IsDomestic>false</IsDomestic>
<NoOfRooms>0</NoOfRooms>
<NoOfNights>0</NoOfNights>
<IsTBOMapped>false</IsTBOMapped>
<SupplierHotelCodes />
<UAPISessionId>7685eeea-c903-4eb7-ae76-2369f40b635c</UAPISessionId>
<HotelDetails>
<StarRating>All</StarRating>
</HotelDetails>
<BookingMode>NotSet</BookingMode>
</HotelSearchResult>
<HotelSearchResult>
.......
</HotelSearchResult>
</ArrayOfHotelSearchResult>
Can anybody help me in making xslt. Currently I an unable to get any value in xslt and i am also facing problem in debugging in VS2012. Sometimes debugger comes sometimes not.
Here is a start using xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication93
{
class Program
{
const string XML_INPUT = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument inputDoc = XDocument.Load(XML_INPUT);
List<XElement> hotelSearchResults = inputDoc.Descendants().Where(x => x.Name.LocalName == "HotelSearchResult").ToList();
XNamespace aNS = hotelSearchResults[0].GetNamespaceOfPrefix("a");
XNamespace bNS = hotelSearchResults[0].GetNamespaceOfPrefix("b");
string header = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
"<ArrayOfHotelSearchResult xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"</ArrayOfHotelSearchResult>";
XDocument doc = XDocument.Parse(header);
XElement arrayOfHotelSearchResult = (XElement)doc.FirstNode;
int count = 0;
foreach (XElement hotelSearchResult in hotelSearchResults)
{
XElement newHotel = new XElement("HotelSearchResult");
arrayOfHotelSearchResult.Add(newHotel);
newHotel.Add(new object[] {
new XElement("ResultIndex", ++count),
new XElement("HotelCode", (string)hotelSearchResult.Descendants(bNS + "HotelCode").FirstOrDefault())
});
XElement price = new XElement("Price");
newHotel.Add(price);
price.Add(new XElement("CommissionType", (string)hotelSearchResult.Descendants(bNS + "CommissionType").FirstOrDefault()));
}
}
}
}
Using XmlReader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication93
{
class Program
{
const string XML_INPUT = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(XML_INPUT);
XNamespace bNS = "PricingCommonAPI";
while(!reader.EOF)
{
if(reader.Name != "HotelSearchResult")
{
reader.ReadToFollowing("HotelSearchResult", bNS.NamespaceName);
}
if(!reader.EOF)
{
XElement hotelSearchResult = (XElement)XElement.ReadFrom(reader);
}
}
}
}
}
XSLT is a type of stylesheet primarily used to present the data from XML in a human-readable way, usually by putting it into HTML elements.
If you want to convert your existing XML to a new structure, I suggest you use XML tools in C# to deserialize the XML, map it to the new structure and render a new XML.