hoping someone knows how to work with this xml stucture. I'm trying to deserialize a xml file of some game.
I have the following file:
<?xml version="1.0" ?>
<GameUI>
<XML_Version Ver="1205" />
<State id="100000" fontcnt="0" texturecnt="0" nodename="SCENE_COMMON_AGORA">
<Font id="0" height="10" weight="100" name="TEST" size="100" filename="TEST" facename="TEST" />
<Texture id="2818432626" name="effect_01.dds" height="256" width="512" filename="effect_01" />
<Component id="610" type="15" font="0" texture="2818432626" nodename="bat_userback_start" texcoord="1.32.13.60" textformat="0" alpa="235" fontcolor="0" SourceType="0" textureNodeName="32" folderid="0" enable="1" />
<Font id="2" height="-12" weight="500" name="TEST Regular" size="90" filename="TEST Regular_9_w500pt" facename="TEST Regular" />
<Dialog nodename="E_DLG_AGORA_MENU" id="101000" active="0" coordinatetype="1" x="0" y="0" w="1024" h="0" ch="0" caption="1" minimize="0" de_caption="29" de_body="29" de_bodywide16_9="29" de_bodywide16_10="29" ToolVisible="1">
<Static nodename="E_DLG_AGORA_MENU_STC_NOTICE" ScaleNormal="default" ScalePress="default" ScaleSelect="default" ScaleOver="default" ScaleDisable="default" ScaleAppear="default" ScaleDisappear="default" layerlevel="0" id="101071" coordinatetype="1" renderlevel="0" appeartime="0" disappeartime="0" appeargroup="0" disappeargroup="0" ctrlAlignType="0" alignOffset="0" fonttype="0" fontColorNormal="-269516" fontColorSelect="-16777216" fontColorOver="-16777216" fontColorDisable="-16777216" fontColorPress="-16777216" x="81" y="39" w="296" h="64" enable="1" ToolVisible="1" Element_0="4" shadow="0" text="" TextWAlign="1" TextHAlign="0" DrawUseTexture="0" TextPosX="0" TextPosY="0" display_font="216">Static</Static>
<Static nodename="E_DLG_AGORA_MENU_STC_PLAYER_INFO_TIP" ScaleNormal="default" ScalePress="default" ScaleSelect="default" ScaleOver="default" ScaleDisable="default" ScaleAppear="default" ScaleDisappear="default" layerlevel="7" id="101072" coordinatetype="1" renderlevel="1" appeartime="0" disappeartime="0" appeargroup="0" disappeargroup="0" ctrlAlignType="0" alignOffset="0" fonttype="0" fontColorNormal="-14015975" fontColorSelect="-14015975" fontColorOver="-14015975" fontColorDisable="-14015975" fontColorPress="-14015975" x="9" y="799" w="75" h="20" enable="1" ToolVisible="1" Element_0="4" shadow="0" text="Static" TextWAlign="1" TextHAlign="4" DrawUseTexture="0" TextPosX="0" TextPosY="0" display_font="219">Static</Static>
<Static nodename="E_DLG_AGORA_MENU_STC_POINT" ScaleNormal="default" ScalePress="default" ScaleSelect="default" ScaleOver="default" ScaleDisable="default" ScaleAppear="default" ScaleDisappear="default" layerlevel="2" id="101073" coordinatetype="1" renderlevel="0" appeartime="0" disappeartime="0" appeargroup="0" disappeargroup="0" ctrlAlignType="3" alignOffset="120" fonttype="0" fontColorNormal="-1381654" fontColorSelect="-16777216" fontColorOver="-16777216" fontColorDisable="-16777216" fontColorPress="-16777216" x="587" y="747" w="90" h="20" enable="1" ToolVisible="1" Element_0="4" shadow="0" text="0" TextWAlign="0" TextHAlign="4" DrawUseTexture="0" TextPosX="0" TextPosY="0" display_font="216">Static</Static>
</Dialog>
</State>
</GameUI>
I used https://xmltocsharp.azurewebsites.net/ to create the struct of the xml.
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="XML_Version")]
public class XML_Version {
[XmlAttribute(AttributeName="Ver")]
public string Ver { get; set; }
}
[XmlRoot(ElementName="Font")]
public class Font {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="height")]
public string Height { get; set; }
[XmlAttribute(AttributeName="weight")]
public string Weight { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="size")]
public string Size { get; set; }
[XmlAttribute(AttributeName="filename")]
public string Filename { get; set; }
[XmlAttribute(AttributeName="facename")]
public string Facename { get; set; }
}
[XmlRoot(ElementName="Texture")]
public class Texture {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="height")]
public string Height { get; set; }
[XmlAttribute(AttributeName="width")]
public string Width { get; set; }
[XmlAttribute(AttributeName="filename")]
public string Filename { get; set; }
}
[XmlRoot(ElementName="Component")]
public class Component {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="type")]
public string Type { get; set; }
[XmlAttribute(AttributeName="font")]
public string Font { get; set; }
[XmlAttribute(AttributeName="texture")]
public string Texture { get; set; }
[XmlAttribute(AttributeName="nodename")]
public string Nodename { get; set; }
[XmlAttribute(AttributeName="texcoord")]
public string Texcoord { get; set; }
[XmlAttribute(AttributeName="textformat")]
public string Textformat { get; set; }
[XmlAttribute(AttributeName="alpa")]
public string Alpa { get; set; }
[XmlAttribute(AttributeName="fontcolor")]
public string Fontcolor { get; set; }
[XmlAttribute(AttributeName="SourceType")]
public string SourceType { get; set; }
[XmlAttribute(AttributeName="textureNodeName")]
public string TextureNodeName { get; set; }
[XmlAttribute(AttributeName="folderid")]
public string Folderid { get; set; }
[XmlAttribute(AttributeName="enable")]
public string Enable { get; set; }
}
[XmlRoot(ElementName="Static")]
public class Static {
[XmlAttribute(AttributeName="nodename")]
public string Nodename { get; set; }
[XmlAttribute(AttributeName="ScaleNormal")]
public string ScaleNormal { get; set; }
[XmlAttribute(AttributeName="ScalePress")]
public string ScalePress { get; set; }
[XmlAttribute(AttributeName="ScaleSelect")]
public string ScaleSelect { get; set; }
[XmlAttribute(AttributeName="ScaleOver")]
public string ScaleOver { get; set; }
[XmlAttribute(AttributeName="ScaleDisable")]
public string ScaleDisable { get; set; }
[XmlAttribute(AttributeName="ScaleAppear")]
public string ScaleAppear { get; set; }
[XmlAttribute(AttributeName="ScaleDisappear")]
public string ScaleDisappear { get; set; }
[XmlAttribute(AttributeName="layerlevel")]
public string Layerlevel { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="coordinatetype")]
public string Coordinatetype { get; set; }
[XmlAttribute(AttributeName="renderlevel")]
public string Renderlevel { get; set; }
[XmlAttribute(AttributeName="appeartime")]
public string Appeartime { get; set; }
[XmlAttribute(AttributeName="disappeartime")]
public string Disappeartime { get; set; }
[XmlAttribute(AttributeName="appeargroup")]
public string Appeargroup { get; set; }
[XmlAttribute(AttributeName="disappeargroup")]
public string Disappeargroup { get; set; }
[XmlAttribute(AttributeName="ctrlAlignType")]
public string CtrlAlignType { get; set; }
[XmlAttribute(AttributeName="alignOffset")]
public string AlignOffset { get; set; }
[XmlAttribute(AttributeName="fonttype")]
public string Fonttype { get; set; }
[XmlAttribute(AttributeName="fontColorNormal")]
public string FontColorNormal { get; set; }
[XmlAttribute(AttributeName="fontColorSelect")]
public string FontColorSelect { get; set; }
[XmlAttribute(AttributeName="fontColorOver")]
public string FontColorOver { get; set; }
[XmlAttribute(AttributeName="fontColorDisable")]
public string FontColorDisable { get; set; }
[XmlAttribute(AttributeName="fontColorPress")]
public string FontColorPress { get; set; }
[XmlAttribute(AttributeName="x")]
public string X { get; set; }
[XmlAttribute(AttributeName="y")]
public string Y { get; set; }
[XmlAttribute(AttributeName="w")]
public string W { get; set; }
[XmlAttribute(AttributeName="h")]
public string H { get; set; }
[XmlAttribute(AttributeName="enable")]
public string Enable { get; set; }
[XmlAttribute(AttributeName="ToolVisible")]
public string ToolVisible { get; set; }
[XmlAttribute(AttributeName="Element_0")]
public string Element_0 { get; set; }
[XmlAttribute(AttributeName="shadow")]
public string Shadow { get; set; }
[XmlAttribute(AttributeName="text")]
public string _Text { get; set; }
[XmlText]
public string __Text { get; set; }
[XmlAttribute(AttributeName="TextWAlign")]
public string TextWAlign { get; set; }
[XmlAttribute(AttributeName="TextHAlign")]
public string TextHAlign { get; set; }
[XmlAttribute(AttributeName="DrawUseTexture")]
public string DrawUseTexture { get; set; }
[XmlAttribute(AttributeName="TextPosX")]
public string TextPosX { get; set; }
[XmlAttribute(AttributeName="TextPosY")]
public string TextPosY { get; set; }
[XmlAttribute(AttributeName="display_font")]
public string Display_font { get; set; }
}
[XmlRoot(ElementName="Dialog")]
public class Dialog {
[XmlElement(ElementName="Static")]
public List<Static> Static { get; set; }
[XmlAttribute(AttributeName="nodename")]
public string Nodename { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="active")]
public string Active { get; set; }
[XmlAttribute(AttributeName="coordinatetype")]
public string Coordinatetype { get; set; }
[XmlAttribute(AttributeName="x")]
public string X { get; set; }
[XmlAttribute(AttributeName="y")]
public string Y { get; set; }
[XmlAttribute(AttributeName="w")]
public string W { get; set; }
[XmlAttribute(AttributeName="h")]
public string H { get; set; }
[XmlAttribute(AttributeName="ch")]
public string Ch { get; set; }
[XmlAttribute(AttributeName="caption")]
public string Caption { get; set; }
[XmlAttribute(AttributeName="minimize")]
public string Minimize { get; set; }
[XmlAttribute(AttributeName="de_caption")]
public string De_caption { get; set; }
[XmlAttribute(AttributeName="de_body")]
public string De_body { get; set; }
[XmlAttribute(AttributeName="de_bodywide16_9")]
public string De_bodywide16_9 { get; set; }
[XmlAttribute(AttributeName="de_bodywide16_10")]
public string De_bodywide16_10 { get; set; }
[XmlAttribute(AttributeName="ToolVisible")]
public string ToolVisible { get; set; }
}
[XmlRoot(ElementName="State")]
public class State {
[XmlElement(ElementName="Font")]
public List<Font> Font { get; set; }
[XmlElement(ElementName="Texture")]
public Texture Texture { get; set; }
[XmlElement(ElementName="Component")]
public Component Component { get; set; }
[XmlElement(ElementName="Dialog")]
public Dialog Dialog { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="fontcnt")]
public string Fontcnt { get; set; }
[XmlAttribute(AttributeName="texturecnt")]
public string Texturecnt { get; set; }
[XmlAttribute(AttributeName="nodename")]
public string Nodename { get; set; }
}
[XmlRoot(ElementName="GameUI")]
public class GameUI {
[XmlElement(ElementName="XML_Version")]
public XML_Version XML_Version { get; set; }
[XmlElement(ElementName="State")]
public State State { get; set; }
}
}
My problem is, I can't figure out how to deserialize it so I can access all the single attributes etc.
to make deserialize to this XML you need to use:
using System.Xml.Serialization;
and the following code:
private void btnConvert_Click(object sender, EventArgs e)
{
string xmlString = tbInput.Text;
var mySerializer = new XmlSerializer(typeof(GameUI));
GameUI gameUI;
using (StringReader reader = new StringReader(xmlString))
{
gameUI = (GameUI)mySerializer.Deserialize(reader);
"gameUI.State.Id.ToString() - " +gameUI.State.Id.ToString();
}
}
and the result:
The classes you can use this website:
XML to C# Class
You need a root class for the xml
public class Root
{
[JsonProperty("?xml")]
public XML_Version Xml { get; set; }
public GameUI GameUI { get; set; }
}
So using Newtonsoft you can do something like this
using Newtonsoft.Json;
public void Test(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
var xMLObject = JsonConvert.DeserializeObject<Root>(jsonText);
}
Related
I have a XML from that i want to get all node value. But after Deserialize i am getting null in my order node and inner node, can you please tell me what mistake I am doing.
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<Order Notes="Test Order" PackageTypeID="0" Service="Quote" CallerPhone="" CustomerCode="GA" Caller="" CalledBy="" CheckPiecesWeight="Y" ForceReRateFlag="True" RouteNumber="" OrderDate="08/04/2017" Auth="" Requestor="CVS - Lenexa" UserGUID="{788888-4555-4444-5455-55555}" GetUserGUIDDataCalledBy="Oreser" WebUserID="254282" UserName="TREW" ChangeOps="0" OrderID="1803019" OrderGUID="{6454546-ABD0-91CA-841D75A363CB}" Origin="W" OrderNumber="2718140" OrderDateTime="08/04/2017 10:00" CreateDate="08/09/17 00:31" DimWeightFactor="0" Pieces="1" Weight="0" RouteZone="LAX" CustomerGroup="" ServiceID="0" TotalMiles="0" AmountCharged="$0.00" DriverPayXML="1" CarrierTypeID="1" DueDateTime="8/4/2017 10:00:00 AM" PickedUpDateTime="" DeliveredDateTime="" OperationalStatus="N" saveorderpieceactionstatus="0" AmountChargedChanged="True">
<Stops>
<Stop Sequence="1" StopType="P" Note="" Name="UPS" Address="Adreena St" City="Vansrn NIRC" State="CA" Zip="98741" Phone="" EarlyDateTime="" ScheduledDateTime="8/4/2017 10:00:00 AM" LateDateTime="" AVSQuality="1" Latitude="74.213827" Longitude="-418.481529" DispatchZoneFound="True" Pieces="1" Weight="0">
<OrderStopPieces>
<OrderStopPiece Sequence="1" PieceAction="P" PieceID="714" OrderStopPieceID="1444"/>
</OrderStopPieces>
</Stop>
<Stop Sequence="2" StopType="D" Note="" Name="CCR Nicla" Address="S.BOSE STREET" City="ARI GUTS" State="CA" Zip="94125" Phone="1 (800) 360-0520" EarlyDateTime="" ScheduledDateTime="8/4/2017 10:00:00 AM" LateDateTime="" AVSQuality="1" Latitude="24.201719" Longitude="-17.491973" DispatchZoneFound="True" Pieces="0" Weight="0">
<OrderStopPieces>
<OrderStopPiece Sequence="1" PieceAction="D" PieceID="714" OrderStopPieceID="144"/>
</OrderStopPieces>
</Stop>
</Stops>
<Pieces>
<Piece Sequence="1" Pieces="1" Weight="" Description="" ContainerReference="" Reference="1111" PieceID="1111"/>
</Pieces>
<OrderEvents>
<OrderEvent EventType="MYOrder" Note="Test" Add="True" EventDateTime="8/9/2017 12:31:08 AM" UserName="MMPIO"/>
<OrderEvent EventType="New" Note="" EventDateTime="8/9/2017 12:21:08 AM" UserName="MMPIO"/>
</OrderEvents>
<Site SiteID="13" CompanyID="1008" SiteCode="MMM" SiteType="C" Name="FOO" DBAName="KMI" Address="JHHHH" City="ARIZoNA" State="AR" Zip="125487" Message=" MUT" ARAccountNumber="15470" ARDeptNumber="741" APAccountNumber="14720" APDeptNumber="1" BankAccountNumber="5001" BankDeptNumber="1" DispatchNote="" PaymentGatewayID="0" MName="" MAddress="" MCity="" MState="" MZip="" LogoImageID="0" SiteStatus="C"/>
<OrderFees>
<OrderFee FeeTitle="TS" FeeCode="VS"/>
<OrderFee FeeTitle="WEEE DDEA" FeeCode="QE"/>
</OrderFees>
<OrderNotifies/>
</Order>
</SOAP:Body>
Class for Deserialize
[XmlRoot(ElementName="OrderStopPiece")]
public class OrderStopPiece {
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="PieceAction")]
public string PieceAction { get; set; }
[XmlAttribute(AttributeName="PieceID")]
public string PieceID { get; set; }
[XmlAttribute(AttributeName="OrderStopPieceID")]
public string OrderStopPieceID { get; set; }
}
[XmlRoot(ElementName="OrderStopPieces")]
public class OrderStopPieces {
[XmlElement(ElementName="OrderStopPiece")]
public OrderStopPiece OrderStopPiece { get; set; }
}
[XmlRoot(ElementName="Stop")]
public class Stop {
[XmlElement(ElementName="OrderStopPieces")]
public OrderStopPieces OrderStopPieces { get; set; }
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="StopType")]
public string StopType { get; set; }
[XmlAttribute(AttributeName="Note")]
public string Note { get; set; }
[XmlAttribute(AttributeName="Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="Address")]
public string Address { get; set; }
[XmlAttribute(AttributeName="City")]
public string City { get; set; }
[XmlAttribute(AttributeName="State")]
public string State { get; set; }
[XmlAttribute(AttributeName="Zip")]
public string Zip { get; set; }
[XmlAttribute(AttributeName="Phone")]
public string Phone { get; set; }
[XmlAttribute(AttributeName="EarlyDateTime")]
public string EarlyDateTime { get; set; }
[XmlAttribute(AttributeName="ScheduledDateTime")]
public string ScheduledDateTime { get; set; }
[XmlAttribute(AttributeName="LateDateTime")]
public string LateDateTime { get; set; }
[XmlAttribute(AttributeName="AVSQuality")]
public string AVSQuality { get; set; }
[XmlAttribute(AttributeName="Latitude")]
public string Latitude { get; set; }
[XmlAttribute(AttributeName="Longitude")]
public string Longitude { get; set; }
[XmlAttribute(AttributeName="DispatchZoneFound")]
public string DispatchZoneFound { get; set; }
[XmlAttribute(AttributeName="Pieces")]
public string Pieces { get; set; }
[XmlAttribute(AttributeName="Weight")]
public string Weight { get; set; }
}
[XmlRoot(ElementName="Stops")]
public class Stops {
[XmlElement(ElementName="Stop")]
public List<Stop> Stop { get; set; }
}
[XmlRoot(ElementName="Piece")]
public class Piece {
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="Pieces")]
public string Pieces { get; set; }
[XmlAttribute(AttributeName="Weight")]
public string Weight { get; set; }
[XmlAttribute(AttributeName="Description")]
public string Description { get; set; }
[XmlAttribute(AttributeName="ContainerReference")]
public string ContainerReference { get; set; }
[XmlAttribute(AttributeName="Reference")]
public string Reference { get; set; }
[XmlAttribute(AttributeName="PieceID")]
public string PieceID { get; set; }
}
[XmlRoot(ElementName="Pieces")]
public class Pieces {
[XmlElement(ElementName="Piece")]
public Piece Piece { get; set; }
}
[XmlRoot(ElementName="OrderEvent")]
public class OrderEvent {
[XmlAttribute(AttributeName="EventType")]
public string EventType { get; set; }
[XmlAttribute(AttributeName="Note")]
public string Note { get; set; }
[XmlAttribute(AttributeName="Add")]
public string Add { get; set; }
[XmlAttribute(AttributeName="EventDateTime")]
public string EventDateTime { get; set; }
[XmlAttribute(AttributeName="UserName")]
public string UserName { get; set; }
}
[XmlRoot(ElementName="OrderEvents")]
public class OrderEvents {
[XmlElement(ElementName="OrderEvent")]
public List<OrderEvent> OrderEvent { get; set; }
}
[XmlRoot(ElementName="Site")]
public class Site {
[XmlAttribute(AttributeName="SiteID")]
public string SiteID { get; set; }
[XmlAttribute(AttributeName="CompanyID")]
public string CompanyID { get; set; }
[XmlAttribute(AttributeName="SiteCode")]
public string SiteCode { get; set; }
[XmlAttribute(AttributeName="SiteType")]
public string SiteType { get; set; }
[XmlAttribute(AttributeName="Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="DBAName")]
public string DBAName { get; set; }
[XmlAttribute(AttributeName="Address")]
public string Address { get; set; }
[XmlAttribute(AttributeName="City")]
public string City { get; set; }
[XmlAttribute(AttributeName="State")]
public string State { get; set; }
[XmlAttribute(AttributeName="Zip")]
public string Zip { get; set; }
[XmlAttribute(AttributeName="Message")]
public string Message { get; set; }
[XmlAttribute(AttributeName="ARAccountNumber")]
public string ARAccountNumber { get; set; }
[XmlAttribute(AttributeName="ARDeptNumber")]
public string ARDeptNumber { get; set; }
[XmlAttribute(AttributeName="APAccountNumber")]
public string APAccountNumber { get; set; }
[XmlAttribute(AttributeName="APDeptNumber")]
public string APDeptNumber { get; set; }
[XmlAttribute(AttributeName="BankAccountNumber")]
public string BankAccountNumber { get; set; }
[XmlAttribute(AttributeName="BankDeptNumber")]
public string BankDeptNumber { get; set; }
[XmlAttribute(AttributeName="DispatchNote")]
public string DispatchNote { get; set; }
[XmlAttribute(AttributeName="PaymentGatewayID")]
public string PaymentGatewayID { get; set; }
[XmlAttribute(AttributeName="MName")]
public string MName { get; set; }
[XmlAttribute(AttributeName="MAddress")]
public string MAddress { get; set; }
[XmlAttribute(AttributeName="MCity")]
public string MCity { get; set; }
[XmlAttribute(AttributeName="MState")]
public string MState { get; set; }
[XmlAttribute(AttributeName="MZip")]
public string MZip { get; set; }
[XmlAttribute(AttributeName="LogoImageID")]
public string LogoImageID { get; set; }
[XmlAttribute(AttributeName="SiteStatus")]
public string SiteStatus { get; set; }
}
[XmlRoot(ElementName="OrderFee")]
public class OrderFee {
[XmlAttribute(AttributeName="FeeTitle")]
public string FeeTitle { get; set; }
[XmlAttribute(AttributeName="FeeCode")]
public string FeeCode { get; set; }
}
[XmlRoot(ElementName="OrderFees")]
public class OrderFees {
[XmlElement(ElementName="OrderFee")]
public List<OrderFee> OrderFee { get; set; }
}
[XmlRoot(ElementName="Order")]
public class Order {
[XmlElement(ElementName="Stops")]
public Stops Stops { get; set; }
[XmlElement(ElementName="Pieces")]
public Pieces Pieces { get; set; }
[XmlAttribute(AttributeName="Pieces")]
public string _Pieces { get; set; }
[XmlElement(ElementName="OrderEvents")]
public OrderEvents OrderEvents { get; set; }
[XmlElement(ElementName="Site")]
public Site Site { get; set; }
[XmlElement(ElementName="OrderFees")]
public OrderFees OrderFees { get; set; }
[XmlElement(ElementName="OrderNotifies")]
public string OrderNotifies { get; set; }
[XmlAttribute(AttributeName="Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName="PackageTypeID")]
public string PackageTypeID { get; set; }
[XmlAttribute(AttributeName="Service")]
public string Service { get; set; }
[XmlAttribute(AttributeName="CallerPhone")]
public string CallerPhone { get; set; }
[XmlAttribute(AttributeName="CustomerCode")]
public string CustomerCode { get; set; }
[XmlAttribute(AttributeName="Caller")]
public string Caller { get; set; }
[XmlAttribute(AttributeName="CalledBy")]
public string CalledBy { get; set; }
[XmlAttribute(AttributeName="CheckPiecesWeight")]
public string CheckPiecesWeight { get; set; }
[XmlAttribute(AttributeName="ForceReRateFlag")]
public string ForceReRateFlag { get; set; }
[XmlAttribute(AttributeName="RouteNumber")]
public string RouteNumber { get; set; }
[XmlAttribute(AttributeName="OrderDate")]
public string OrderDate { get; set; }
[XmlAttribute(AttributeName="Auth")]
public string Auth { get; set; }
[XmlAttribute(AttributeName="Requestor")]
public string Requestor { get; set; }
[XmlAttribute(AttributeName="UserGUID")]
public string UserGUID { get; set; }
[XmlAttribute(AttributeName="GetUserGUIDDataCalledBy")]
public string GetUserGUIDDataCalledBy { get; set; }
[XmlAttribute(AttributeName="WebUserID")]
public string WebUserID { get; set; }
[XmlAttribute(AttributeName="UserName")]
public string UserName { get; set; }
[XmlAttribute(AttributeName="ChangeOps")]
public string ChangeOps { get; set; }
[XmlAttribute(AttributeName="OrderID")]
public string OrderID { get; set; }
[XmlAttribute(AttributeName="OrderGUID")]
public string OrderGUID { get; set; }
[XmlAttribute(AttributeName="Origin")]
public string Origin { get; set; }
[XmlAttribute(AttributeName="OrderNumber")]
public string OrderNumber { get; set; }
[XmlAttribute(AttributeName="OrderDateTime")]
public string OrderDateTime { get; set; }
[XmlAttribute(AttributeName="CreateDate")]
public string CreateDate { get; set; }
[XmlAttribute(AttributeName="DimWeightFactor")]
public string DimWeightFactor { get; set; }
[XmlAttribute(AttributeName="Weight")]
public string Weight { get; set; }
[XmlAttribute(AttributeName="RouteZone")]
public string RouteZone { get; set; }
[XmlAttribute(AttributeName="CustomerGroup")]
public string CustomerGroup { get; set; }
[XmlAttribute(AttributeName="ServiceID")]
public string ServiceID { get; set; }
[XmlAttribute(AttributeName="TotalMiles")]
public string TotalMiles { get; set; }
[XmlAttribute(AttributeName="AmountCharged")]
public string AmountCharged { get; set; }
[XmlAttribute(AttributeName="DriverPayXML")]
public string DriverPayXML { get; set; }
[XmlAttribute(AttributeName="CarrierTypeID")]
public string CarrierTypeID { get; set; }
[XmlAttribute(AttributeName="DueDateTime")]
public string DueDateTime { get; set; }
[XmlAttribute(AttributeName="PickedUpDateTime")]
public string PickedUpDateTime { get; set; }
[XmlAttribute(AttributeName="DeliveredDateTime")]
public string DeliveredDateTime { get; set; }
[XmlAttribute(AttributeName="OperationalStatus")]
public string OperationalStatus { get; set; }
[XmlAttribute(AttributeName="saveorderpieceactionstatus")]
public string Saveorderpieceactionstatus { get; set; }
[XmlAttribute(AttributeName="AmountChargedChanged")]
public string AmountChargedChanged { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="Order")]
public Order Order { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="SOAP", Namespace="http://www.w3.org/2000/xmlns/")]
public string SOAP { get; set; }
}
my code :
// response xml in str
XmlSerializer serializer = new XmlSerializer(typeof(SaveResponseObject.Envelope));
using (TextReader reader = new StringReader(str))
{
SaveResponseObject.Envelope result = (SaveResponseObject.Envelope)serializer.Deserialize(reader);
}
Add Namespace = "" to Order property in Body class. Should be ok.
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "Order", Namespace = "")]
public Order Order { get; set; }
}
Use this attribute to decorate over order
[XmlElement(ElementName = "Order", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
I deserialize complex JSON (Spotify Playlist) and get root level values but I cannot get branches values. I Google problem, try different options without success but, I assume its silly mistake or lack of knowledge so therefore just ask for help what I am missing?
My classes are:
public class Playlist
{
public string collaborative { get; set; }
public string description { get; set; }
//public <ExternalUrls> external_urls { get; set; } //object
//public List<Followers> followers { get; set; } //object
public string href { get; set; }
public string id { get; set; }
//public List<Image> images { get; set; } //array
public string name { get; set; }
}
public class Tracks
{
public string href { get; set; }
public Item items { get; set; } //object
public string limit { get; set; }
public string next { get; set; }
public string offset { get; set; }
public string previous { get; set; }
public string total { get; set; }
}
And code to deserialize looks like that:
StreamReader responseFromServer = new StreamReader(myWebResponse.GetResponseStream());
var dataResponse = responseFromServer.ReadToEnd();
responseFromServer.Close();
var elements = new JavaScriptSerializer().Deserialize<Playlist>(dataResponse);
RootBuffer.AddRow();
RootBuffer.collaborative = elements.collaborative.ToString();
foreach (Tracks trs in elements.tracks)
{
TracksBuffer.AddRow();
TracksBuffer.href = trs.href.ToString()
}
I genereated the classes using this excellent site: json2csharp.com
And used your existing deserialization code successfully with the following classes. It populated all the data including the child collections (brace yourself, it's a long one):
public class Playlist
{
public bool collaborative { get; set; }
public string description { get; set; }
public ExternalUrls external_urls { get; set; }
public Followers followers { get; set; }
public string href { get; set; }
public string id { get; set; }
public List<Image> images { get; set; }
public string name { get; set; }
public Owner owner { get; set; }
public bool #public { get; set; }
public string snapshot_id { get; set; }
public Tracks tracks { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls
{
public string spotify { get; set; }
}
public class Followers
{
public object href { get; set; }
public int total { get; set; }
}
public class Image
{
public object height { get; set; }
public string url { get; set; }
public object width { get; set; }
}
public class ExternalUrls2
{
public string spotify { get; set; }
}
public class Owner
{
public ExternalUrls2 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls3
{
public string spotify { get; set; }
}
public class AddedBy
{
public ExternalUrls3 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls4
{
public string spotify { get; set; }
}
public class Image2
{
public int height { get; set; }
public string url { get; set; }
public int width { get; set; }
}
public class Album
{
public string album_type { get; set; }
public List<object> available_markets { get; set; }
public ExternalUrls4 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public List<Image2> images { get; set; }
public string name { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls5
{
public string spotify { get; set; }
}
public class Artist
{
public ExternalUrls5 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalIds
{
public string isrc { get; set; }
}
public class ExternalUrls6
{
public string spotify { get; set; }
}
public class Track
{
public Album album { get; set; }
public List<Artist> artists { get; set; }
public List<object> available_markets { get; set; }
public int disc_number { get; set; }
public int duration_ms { get; set; }
public bool #explicit { get; set; }
public ExternalIds external_ids { get; set; }
public ExternalUrls6 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string name { get; set; }
public int popularity { get; set; }
public string preview_url { get; set; }
public int track_number { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class Item
{
public string added_at { get; set; }
public AddedBy added_by { get; set; }
public bool is_local { get; set; }
public Track track { get; set; }
}
public class Tracks
{
public string href { get; set; }
public List<Item> items { get; set; }
public int limit { get; set; }
public object next { get; set; }
public int offset { get; set; }
public object previous { get; set; }
public int total { get; set; }
}
Hope this helps.
I have the following class object structure:
public class StatusObj3
{
public int status { get; set; }
public DataObj3 data { get; set; }
}
public class DataObj3
{
public string survey_id { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int custom_variable_count { get; set; }
public List<Custom_VariablesObj> custom_variables { get; set; }
public int language_id { get; set; }
public int num_responses { get; set; }
public int question_count { get; set; }
public string nickname { get; set; }
public TitleObj title { get; set; }
public List<PagesObj> pages { get; set; }
}
public class Custom_VariablesObj
{
public string variable_label { get; set; }
public string question_id { get; set; }
}
public class TitleObj
{
public bool enabled { get; set; }
public string text { get; set; }
}
public class PagesObj
{
string page_id { get; set; }
string heading { get; set; }
string sub_heading { get; set; }
public List<QuestionObj> questions { get; set; }
}
public class QuestionObj
{
public string question_id { get; set; }
public string heading { get; set; }
public string position { get; set; }
public QuestionTypeObj type { get; set; }
public List<AnswerObj> answers { get; set; }
}
public class QuestionTypeObj
{
public string family { get; set; }
public string subtype { get; set; }
}
public class AnswerObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int weight { get; set; }
public bool apply_all_rows { get; set; }
public bool is_answer { get; set; }
public List<ItemsObj> items { get; set; }
}
public class ItemsObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string type { get; set; }
public string text { get; set; }
}
I 've deserialized json into this object via:
var results_SurveyDetails = deserializer.Deserialize<StatusObj3>(json_returned);
I'm trying to loop thru the pages by:
foreach (var page in results_SurveyDetails.data.pages)
However, not all members of page are available to use.
Only the page.questions is there and not page_id, heading and subheading.
What am I doing wrong?
You are missing public on your other properties in class PagesObj.
I am using the following tutorial to parse a JSON document.
http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx
The JSON document that I am trying to parse can be accessed here:
http://www.visitproject.co.uk/Tweets/Ireland.txt
JavaScriptSerializer jss = new JavaScriptSerializer();
jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
dynamic tweets = jss.Deserialize(json, typeof(object)) as dynamic;
foreach (var tweettext in tweets.statuses.text)
{
Console.WriteLine("Tweet: " + tweettext);
}
I am able to perform a watch on tweets.statuses and it does contain a collection of tweets. I would like to get the text value from each tweet. The only thing I can see that is different for the tutorial is that it is an array in JSON and I expect that this is why it is not working. Does anyone have any ideas? Thank you for your help!
You could use LINQ to JSON, like this:
// Parse JSON
JObject o = JObject.Parse(json);
Read LINQ to JSON documentation for details on how to query for the pieces of JSON you want.
You can simply copy paste the code below and get your answer.
This is how i parsed your json data.
I created classes based on your json
public class Metadata
{
public string result_type { get; set; }
public string iso_language_code { get; set; }
}
public class Url2
{
public string url { get; set; }
public string expanded_url { get; set; }
public string display_url { get; set; }
public List<int> indices { get; set; }
}
public class Url
{
public List<Url2> urls { get; set; }
}
public class Description
{
public List<object> urls { get; set; }
}
public class Entities
{
public Url url { get; set; }
public Description description { get; set; }
}
public class User
{
public int id { get; set; }
public string id_str { get; set; }
public string name { get; set; }
public string screen_name { get; set; }
public string location { get; set; }
public string description { get; set; }
public string url { get; set; }
public Entities entities { get; set; }
public bool #protected { get; set; }
public int followers_count { get; set; }
public int friends_count { get; set; }
public int listed_count { get; set; }
public string created_at { get; set; }
public int favourites_count { get; set; }
public int? utc_offset { get; set; }
public string time_zone { get; set; }
public bool geo_enabled { get; set; }
public bool verified { get; set; }
public int statuses_count { get; set; }
public string lang { get; set; }
public bool contributors_enabled { get; set; }
public bool is_translator { get; set; }
public string profile_background_color { get; set; }
public string profile_background_image_url { get; set; }
public string profile_background_image_url_https { get; set; }
public bool profile_background_tile { get; set; }
public string profile_image_url { get; set; }
public string profile_image_url_https { get; set; }
public string profile_link_color { get; set; }
public string profile_sidebar_border_color { get; set; }
public string profile_sidebar_fill_color { get; set; }
public string profile_text_color { get; set; }
public bool profile_use_background_image { get; set; }
public bool default_profile { get; set; }
public bool default_profile_image { get; set; }
public bool following { get; set; }
public bool follow_request_sent { get; set; }
public bool notifications { get; set; }
public string profile_banner_url { get; set; }
}
public class Large
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Medium2
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Thumb
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Small
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Sizes
{
public Large large { get; set; }
public Medium2 medium { get; set; }
public Thumb thumb { get; set; }
public Small small { get; set; }
}
public class Medium
{
public object id { get; set; }
public string id_str { get; set; }
public List<int> indices { get; set; }
public string media_url { get; set; }
public string media_url_https { get; set; }
public string url { get; set; }
public string display_url { get; set; }
public string expanded_url { get; set; }
public string type { get; set; }
public Sizes sizes { get; set; }
public long source_status_id { get; set; }
public string source_status_id_str { get; set; }
}
public class Entities2
{
public List<object> hashtags { get; set; }
public List<object> symbols { get; set; }
public List<object> urls { get; set; }
public List<object> user_mentions { get; set; }
public List<Medium> media { get; set; }
}
public class Metadata2
{
public string result_type { get; set; }
public string iso_language_code { get; set; }
}
public class Description2
{
public List<object> urls { get; set; }
}
public class Url4
{
public string url { get; set; }
public string expanded_url { get; set; }
public string display_url { get; set; }
public List<int> indices { get; set; }
}
public class Url3
{
public List<Url4> urls { get; set; }
}
public class Entities3
{
public Description2 description { get; set; }
public Url3 url { get; set; }
}
public class User2
{
public int id { get; set; }
public string id_str { get; set; }
public string name { get; set; }
public string screen_name { get; set; }
public string location { get; set; }
public string description { get; set; }
public string url { get; set; }
public Entities3 entities { get; set; }
public bool #protected { get; set; }
public int followers_count { get; set; }
public int friends_count { get; set; }
public int listed_count { get; set; }
public string created_at { get; set; }
public int favourites_count { get; set; }
public int utc_offset { get; set; }
public string time_zone { get; set; }
public bool geo_enabled { get; set; }
public bool verified { get; set; }
public int statuses_count { get; set; }
public string lang { get; set; }
public bool contributors_enabled { get; set; }
public bool is_translator { get; set; }
public string profile_background_color { get; set; }
public string profile_background_image_url { get; set; }
public string profile_background_image_url_https { get; set; }
public bool profile_background_tile { get; set; }
public string profile_image_url { get; set; }
public string profile_image_url_https { get; set; }
public string profile_banner_url { get; set; }
public string profile_link_color { get; set; }
public string profile_sidebar_border_color { get; set; }
public string profile_sidebar_fill_color { get; set; }
public string profile_text_color { get; set; }
public bool profile_use_background_image { get; set; }
public bool default_profile { get; set; }
public bool default_profile_image { get; set; }
public bool following { get; set; }
public bool follow_request_sent { get; set; }
public bool notifications { get; set; }
}
public class Medium4
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Large2
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Thumb2
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Small2
{
public int w { get; set; }
public int h { get; set; }
public string resize { get; set; }
}
public class Sizes2
{
public Medium4 medium { get; set; }
public Large2 large { get; set; }
public Thumb2 thumb { get; set; }
public Small2 small { get; set; }
}
public class Medium3
{
public long id { get; set; }
public string id_str { get; set; }
public List<int> indices { get; set; }
public string media_url { get; set; }
public string media_url_https { get; set; }
public string url { get; set; }
public string display_url { get; set; }
public string expanded_url { get; set; }
public string type { get; set; }
public Sizes2 sizes { get; set; }
}
public class Entities4
{
public List<object> hashtags { get; set; }
public List<object> symbols { get; set; }
public List<object> urls { get; set; }
public List<object> user_mentions { get; set; }
public List<Medium3> media { get; set; }
}
public class RetweetedStatus
{
public Metadata2 metadata { get; set; }
public string created_at { get; set; }
public object id { get; set; }
public string id_str { get; set; }
public string text { get; set; }
public string source { get; set; }
public bool truncated { get; set; }
public long? in_reply_to_status_id { get; set; }
public string in_reply_to_status_id_str { get; set; }
public int? in_reply_to_user_id { get; set; }
public string in_reply_to_user_id_str { get; set; }
public string in_reply_to_screen_name { get; set; }
public User2 user { get; set; }
public object geo { get; set; }
public object coordinates { get; set; }
public object place { get; set; }
public object contributors { get; set; }
public int retweet_count { get; set; }
public int favorite_count { get; set; }
public Entities4 entities { get; set; }
public bool favorited { get; set; }
public bool retweeted { get; set; }
public bool possibly_sensitive { get; set; }
public string lang { get; set; }
}
public class Status
{
public Metadata metadata { get; set; }
public string created_at { get; set; }
public object id { get; set; }
public string id_str { get; set; }
public string text { get; set; }
public string source { get; set; }
public bool truncated { get; set; }
public long? in_reply_to_status_id { get; set; }
public string in_reply_to_status_id_str { get; set; }
public int? in_reply_to_user_id { get; set; }
public string in_reply_to_user_id_str { get; set; }
public string in_reply_to_screen_name { get; set; }
public User user { get; set; }
public object geo { get; set; }
public object coordinates { get; set; }
public object place { get; set; }
public object contributors { get; set; }
public int retweet_count { get; set; }
public int favorite_count { get; set; }
public Entities2 entities { get; set; }
public bool favorited { get; set; }
public bool retweeted { get; set; }
public bool possibly_sensitive { get; set; }
public string lang { get; set; }
public RetweetedStatus retweeted_status { get; set; }
}
public class SearchMetadata
{
public double completed_in { get; set; }
public long max_id { get; set; }
public string max_id_str { get; set; }
public string next_results { get; set; }
public string query { get; set; }
public string refresh_url { get; set; }
public int count { get; set; }
public int since_id { get; set; }
public string since_id_str { get; set; }
}
public class RootObject
{
public List<Status> statuses { get; set; }
public SearchMetadata search_metadata { get; set; }
}
And i parsed your data by using the following method
public void PARSEVALUES(string jsonValue)//jsonValue contains your json
{
JavaScriptSerializer jss = new JavaScriptSerializer();
RootObject r = jss.Deserialize<RootObject>(jsonValue);
foreach (var tweetText in r.statuses)
{
string val = tweetText.text;
}
}
I have the following XML that I am getting back from a WebRequest:
<?xml version="1.0" encoding="UTF-8"?>
<search>
<total_items>5</total_items>
<page_size>10</page_size>
<page_count>1</page_count>
<page_number>1</page_number>
<page_items></page_items>
<first_item></first_item>
<last_item></last_item>
<search_time>0.044</search_time>
<events>
<event id="E0-001-053379749-0">
<title>Antibalas</title>
<url>http://test.com</url>
<description>stuff</description>
<start_time>2013-01-30 20:00:00</start_time>
<stop_time></stop_time>
<tz_id></tz_id>
<tz_olson_path></tz_olson_path>
<tz_country></tz_country>
<tz_city></tz_city>
<venue_id>V0-001-000189211-5</venue_id>
<venue_url>http://blah.com</venue_url>
<venue_name>Troubadour</venue_name>
<venue_display>1</venue_display>
<venue_address>9081 Santa Monica Boulevard</venue_address>
<city_name>West Hollywood</city_name>
<region_name>California</region_name>
<region_abbr>CA</region_abbr>
<postal_code>90069</postal_code>
<country_name>United States</country_name>
<country_abbr2>US</country_abbr2>
<country_abbr>USA</country_abbr>
<latitude>34.0815917</latitude>
<longitude>-118.3892462</longitude>
<geocode_type>EVDB Geocoder</geocode_type>
<all_day>0</all_day>
<recur_string></recur_string>
<calendar_count></calendar_count>
<comment_count></comment_count>
<link_count></link_count>
<going_count></going_count>
<watching_count></watching_count>
<created>2012-12-24 11:40:43</created>
<owner>evdb</owner>
<modified>2013-01-14 21:08:04</modified>
<performers>
<performer>
<id>P0-001-000000517-4</id>
<url>http://test.com</url>
<name>Antibalas</name>
<short_bio>Afro-beat / Funk / Experimental</short_bio>
<creator>jfisher</creator>
<linker>evdb</linker>
</performer>
</performers>
<image>
<url>http://s4.evcdn.com/images/small/I0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url>
<width>48</width>
<height>48</height>
<caption></caption>
<thumb>
<url>http://s4.evcdn.com/images/thumb/I0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://s4.evcdn.com/images/small/I0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://s4.evcdn.com/images/medium/I0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<privacy>1</privacy>
<calendars></calendars>
<groups></groups>
<going></going>
</event>
</events>
</search>
I have created the following classes:
public class EventSearch
{
public int total_items { get; set; }
public int page_size { get; set; }
public int page_count { get; set; }
public int page_number { get; set; }
public int page_items { get; set; }
public string first_item { get; set; }
public string last_item { get; set; }
public string search_time { get; set; }
public List<Event> events { get; set; }
}
public class Event
{
public string id { get; set; }
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public DateTime start_time { get; set; }
public string venue_id { get; set; }
public string venue_url { get; set; }
public string venue_name { get; set; }
public string venue_address { get; set; }
public string city_name { get; set; }
public string region_name { get; set; }
public string region_abbr { get; set; }
public string postal_code { get; set; }
public string country_name { get; set; }
public string country_abbr { get; set; }
public string country_abbr2 { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public List<Performer> performers { get; set; }
public EventImage image { get; set; }
}
public class Performer
{
public string id { get; set; }
public string url { get; set; }
public string name { get; set; }
public string short_bio { get; set; }
}
public class EventImage
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
public Image thumb { get; set; }
public Image small { get; set; }
public Image medium { get; set; }
}
public class Image
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
In the method that I am calling, I want to return a list of events. Here's what I have so far, which is giving me the error:
was not expected.
Code:
var request = WebRequest.Create(url);
var response = request.GetResponse();
if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
{
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader.
StreamReader reader = new StreamReader(dataStream);
XmlSerializer serializer = new XmlSerializer(typeof(EventSearch));
EventSearch deserialized = (EventSearch)serializer.Deserialize(reader);
return deserialized.events;
}
I'm not sure what to do next. Do I need to annotate my classes?
Yes, you will need to add some Attributes to your classes to map the xml elements to your objects.
This should work, However I had to change a few things, The DateTime in the Xml is in the wrong fromat so I made it a string(you can parse as needed), also page_items is an int but it is an empty element in the xml, you wont be able to deserialize this, you will have to change to a string if it has a chance of being empty.
[XmlType("search")]
public class EventSearch
{
public int total_items { get; set; }
public int page_size { get; set; }
public int page_count { get; set; }
public int page_number { get; set; }
// public int? page_items { get; set; }
public string first_item { get; set; }
public string last_item { get; set; }
public string search_time { get; set; }
public List<Event> events { get; set; }
}
[XmlType("event")]
public class Event
{
[XmlAttribute("id")]
public string id { get; set; }
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public string start_time { get; set; }
public string venue_id { get; set; }
public string venue_url { get; set; }
public string venue_name { get; set; }
public string venue_address { get; set; }
public string city_name { get; set; }
public string region_name { get; set; }
public string region_abbr { get; set; }
public string postal_code { get; set; }
public string country_name { get; set; }
public string country_abbr { get; set; }
public string country_abbr2 { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public List<Performer> performers { get; set; }
public EventImage image { get; set; }
}
[XmlType("performer")]
public class Performer
{
public string id { get; set; }
public string url { get; set; }
public string name { get; set; }
public string short_bio { get; set; }
}
[XmlType("image")]
public class EventImage
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
public Image thumb { get; set; }
public Image small { get; set; }
public Image medium { get; set; }
}
[XmlType("thumb")]
public class Image
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
I tested directly from file using:
using (FileStream stream = new FileStream("C:\\Test.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(EventSearch));
EventSearch deserialized = (EventSearch)serializer.Deserialize(stream);
}
But your Stream from the Webresponce should work the same as the FileStream
Hope this helps :)