Formatting issue with a class generated xml in ASP.Net C# - c#

My below my code which generate an xml and return the generated xml but the current format is not the structure expected:
My Class:
public class response
{
[StringLength(64)]
public string reference { get; set; }
public int responseCode { get; set; }
[StringLength(140)]
public string responseMessage { get; set; }
[StringLength(32)]
public string transactionId { get; set; }
public List<account> accounts { get; set; }
}
public class account
{
[StringLength(64)]
public string account_number { get; set; }
}
rs = "<?xml version='1.0' encoding='UTF-8'?><USSDResponse><Status>true</Status><StatusMessage>Account details returned for 08069262257</StatusMessage><SessionID>31853F5C-A1C1-2A6F-E054-8E1F65C33B15</SessionID><AccountNumber><AccountNo>0003893369</AccountNo><AccountStatus>ACCOUNT OPEN REGULAR</AccountStatus><AvailableBalance>17674.69</AvailableBalance><AccountName>IYEKE IKECHUKWU I.</AccountName><AccountCurrency>NGN</AccountCurrency><ProductName>CURRENT STAFF</ProductName></AccountNumber><AccountNumber><AccountNo>0064612613</AccountNo><AccountStatus>ACCOUNT OPEN REGULAR</AccountStatus><AvailableBalance>201132.18</AvailableBalance><AccountName>IKECHUKWU ISRAEL IYEKE</AccountName><AccountCurrency>NGN</AccountCurrency><ProductName>HIDA</ProductName></AccountNumber></USSDResponse>";
x.LoadXml(rs);
status = x.GetElementsByTagName("Status")[0].InnerText;
SessionID = x.GetElementsByTagName("SessionID")[0].InnerText;
if (status != null && status == "true")
{
var accts = x.GetElementsByTagName("AccountNo");
var names = x.GetElementsByTagName("ProductName");
if (accts.Count >= 2)
{
//var AcctNo = new accounts();
foreach (XmlElement a in accts)
{
var acctNo = a.InnerText.Substring(0, 10);
accounts.Add(new account { account_number = acctNo });
}
o.accounts = accounts;
o.reference = reference;
o.responseCode = 6;
o.responseMessage = "Please you can only purchase airtime in naira only and no kobo inclusive.";
o.transactionId = "Nil";
logger.Info($"Wrong amount: {amountString} including kobo entered by the user for mobile number: {msisdn}");
return o;
}
}
Result:
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reference>565695769-8490890112091</reference>
<responseCode>6</responseCode>
<responseMessage>Please you can only purchase airtime in naira only and no kobo inclusive.</responseMessage>
<transactionId>Nil</transactionId>
<accounts>
<account><account_number>0003893369</account_number></account>
<account><account_number>0064612613</account_number></account>
</accounts>
</response>
My above code generate an xml and return the generated xml but the current format is not the structure expected:But I want the result to be exactly with the removal of tag <account_number></account_number>:
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reference>565695769-8490890112091</reference>
<responseCode>6</responseCode>
<responseMessage>Please you can only purchase airtime in naira only and no kobo inclusive.</responseMessage>
<transactionId>Nil</transactionId>
<accounts>
<account>0003893369</account>
<account>0064612613</account>
</accounts>
</response>

Modify your class structure as below,
public class Accounts {
public Accounts ()
{
Account = new List<string>();
}
public List<string> Account { get; set; }
}
public class Response {
...
public Accounts Accounts { get; set; }
}

Related

How to deserialize this xml to objects?

I'm fetching data in the format of this:
<ReplyUserAccount xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" requestid="" version="1.0" xmlns="url">
<Sender partnerid="xx">xx</Sender>
<Users>
<User method="GET" ResultCode="OK" Description="">
<Guid>xx</Guid>
<FirstName>xx</FirstName>
<LastName>xx</LastName>
<Phone>xx</Phone>
<Mobile>xx</Mobile>
<Email>xx</Email>
<EmplNo>xx</EmplNo>
<TacPermission />
<InvPermission>xx</InvPermission>
<CustomerId>xx</CustomerId>
</User>
</Users>
</ReplyUserAccount>
With the following C# objects:
[XmlRoot("ReplyUserAccount")]
public class ReplyUserAccount
{
[XmlElement("Users")]
public Users Users{ get; set; }
}
[XmlType("Users")]
public class Users
{
[XmlElement("User")]
public List<User> UserList{ get; set; }
}
[XmlType("User")]
public class User
{
[XmlElement("EmplNo")]
public string Id{ get; set; }
[XmlElement("Guid")]
public string Guid { get; set; } = null;
[XmlElement("Email")]
public string Email { get; set; }
[XmlElement("FirstName")]
public string FirstName { get; set; }
[XmlElement("LastName")]
public string LastName { get; set; }
public bool Active { get; set; } = true;
public string PhoneNumber { get; set; } = null;
}
And the following deserializing:
var result = await httpClient.GetAsync(url);
var xdoc = XDocument.Parse(await result.Content.ReadAsStringAsync());
XmlSerializer serializer = new XmlSerializer(typeof(ReplyUserAccount));
var content = xdoc.ToString();
TextReader reader = new StringReader(content);
var res = (ReplyUserAccount)serializer.Deserialize(reader);
But I get the following error:
InvalidOperationException: <ReplyUserAccount xmlns='xxx'> was not expected.
I'm a little bit lost as to how to properly deserialize this specific xml data. Any and all help with regards to this is greatly appreciated.
To fix the error, You have to remove xmlns and xsi in xml text before deserialize. You can remove xmlns and xsi like this:
var content = xdoc.ToString();
string strXMLPattern = #"xmlns(:\w+)?=""([^""]+)""|xsi(:\w+)?=""([^""]+)""";
content = Regex.Replace(content, strXMLPattern, "");
Therefore the method should be as follows
var result = await httpClient.GetAsync(url);
var xdoc = XDocument.Parse(await result.Content.ReadAsStringAsync());
XmlSerializer serializer = new XmlSerializer(typeof(ReplyUserAccount));
var content = xdoc.ToString();
string strXMLPattern = #"xmlns(:\w+)?=""([^""]+)""|xsi(:\w+)?=""([^""]+)""";
content = Regex.Replace(content, strXMLPattern, "");
TextReader reader = new StringReader(content);
var res = (ReplyUserAccount)serializer.Deserialize(reader);

XmlSerializer not correct format result

The output i want as below
<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/' >
<SOAP:Body UserGUID = '{redacted}' >
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
<Order UserID = '1' Notes = 'Signature Requiered' CustomerID = '3' >
</Order >
</m:SaveOrder >
</SOAP:Body >
</SOAP:Envelope >
The output xml that i am getting as my result
<?xml version="1.0"?>
<SOAP:Envelope xmlns:m="http://www.e-courier.com/schemas/" xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body UserGUID="">
<m:SaveOrder >
<m:Order UserID="" Notes="" CustomerID="" />
</m:SaveOrder>
</SOAP:Body>
</SOAP:Envelope>
My XML Class code:
[XmlRoot(ElementName="Order")]
public class Order {
[XmlAttribute(AttributeName="UserID")]
public string UserID { get; set; }
[XmlAttribute(AttributeName="Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName="CustomerID")]
public string CustomerID { get; set; }
}
[XmlRoot(ElementName="SaveOrder", Namespace="http://www.e-courier.com/schemas/")]
public class SaveOrder {
[XmlElement(ElementName="Order")]
public Order Order { get; set; }
[XmlAttribute(AttributeName="m", Namespace="http://www.w3.org/2000/xmlns/")]
public string M { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="SaveOrder", Namespace="http://www.e-courier.com/schemas/")]
public SaveOrder SaveOrder { get; set; }
[XmlAttribute(AttributeName="UserGUID")]
public string UserGUID { 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 where i am generating xml
var SaveOrder = new ECSaveOrderRequest.Envelope
{
Body = new ECSaveOrderRequest.Body
{
UserGUID = guid,
SaveOrder = new ECSaveOrderRequest.SaveOrder
{
Order = new ECSaveOrderRequest.Order
{
UserID = Uid,
Notes = "",
CustomerID=""
}
}
}
};
var ns = new XmlSerializerNamespaces();
ns.Add("SOAP", "http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("m", "http://www.e-courier.com/schemas/");
var ser = new XmlSerializer(typeof(ECSaveOrderRequest.Envelope));
using (var ms = new MemoryStream())
{
// write the DTO to the MemoryStream
ser.Serialize(ms, SaveOrder, ns);
using (var wc = new WebClient())
{
wc.Encoding = System.Text.Encoding.UTF8;
ms.Position = 0;
StreamReader stream = new StreamReader(ms);
string requestString = stream.ReadToEnd();
var resp = wc.UploadData(ECUrl, ms.ToArray());
}
}
You need to explicitly clear the xml namespace on SaveOrder.Order or the serializer will default to SaveOrder's xml namespace.
Here you go:
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace ECSaveOrderRequest
{
/*
* <SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/' >
<SOAP:Body UserGUID = '{redacted}' >
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
<Order UserID = '1' Notes = 'Signature Requiered' CustomerID = '3' >
</Order >
</m:SaveOrder >
</SOAP:Body >
</SOAP:Envelope >*/
public class Order
{
[XmlAttribute(AttributeName = "UserID")]
public string UserID { get; set; }
[XmlAttribute(AttributeName = "Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName = "CustomerID")]
public string CustomerID { get; set; }
}
public class SaveOrder
{
[XmlElement(ElementName = "Order", Namespace = "")]
public Order Order { get; set; }
}
public class Body
{
[XmlElement(ElementName = "SaveOrder", Namespace = "http://www.e-courier.com/schemas/")]
public SaveOrder SaveOrder { get; set; }
[XmlAttribute(AttributeName = "UserGUID")]
public string UserGUID { 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; }
}
class Program
{
static void Main(string[] args)
{
var SaveOrder = new ECSaveOrderRequest.Envelope
{
Body = new ECSaveOrderRequest.Body
{
UserGUID = "{redacted}",
SaveOrder = new ECSaveOrderRequest.SaveOrder
{
Order = new ECSaveOrderRequest.Order
{
UserID = "1",
Notes = "Signature Requiered",
CustomerID = "3"
}
}
}
};
var ns = new XmlSerializerNamespaces();
ns.Add("SOAP", "http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("m", "http://www.e-courier.com/schemas/");
var ser = new XmlSerializer(typeof(ECSaveOrderRequest.Envelope));
var ms = new MemoryStream();
// write the DTO to the MemoryStream
ser.Serialize(ms, SaveOrder, ns);
ms.Position = 0;
var xml = Encoding.UTF8.GetString(ms.GetBuffer());
Console.WriteLine(xml);
Console.ReadKey();
}
}
}
outputs
<?xml version="1.0"?>
<SOAP:Envelope xmlns:m="http://www.e-courier.com/schemas/" xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body UserGUID="{redacted}">
<m:SaveOrder>
<Order UserID="1" Notes="Signature Requiered" CustomerID="3" />
</m:SaveOrder>
</SOAP:Body>
</SOAP:Envelope>
Which is a serialization of the same XML document as
<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/' >
<SOAP:Body UserGUID = '{redacted}' >
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
<Order UserID = '1' Notes = 'Signature Requiered' CustomerID = '3' >
</Order >
</m:SaveOrder >
</SOAP:Body>
</SOAP:Envelope>
.

How to Implement In-App Purchases in Windows 10 Apps?

I want to integrate in-app purchasing in my windows universal app. I do the following thing before coding.
Make App on Windows Dev Center
Add products with details in IAPs section and submit to Store as you can see in Image
After that I use the following code in my app to get list of products of In-App purchasing and button to purchase product. I also used CurrentApp instead of CurrentAppSimulator in my code but it goes in exception.
private async void RenderStoreItems()
{
picItems.Clear();
try
{
//StoreManager mySM = new StoreManager();
ListingInformation li = await CurrentAppSimulator.LoadListingInformationAsync();
System.Diagnostics.Debug.WriteLine(li);
foreach (string key in li.ProductListings.Keys)
{
ProductListing pListing = li.ProductListings[key];
System.Diagnostics.Debug.WriteLine(key);
string status = CurrentAppSimulator.LicenseInformation.ProductLicenses[key].IsActive ? "Purchased" : pListing.FormattedPrice;
string imageLink = string.Empty;
picItems.Add(
new ProductItem
{
imgLink = key.Equals("BaazarMagzine101") ? "block-ads.png" : "block-ads.png",
Name = pListing.Name,
Status = status,
key = key,
BuyNowButtonVisible = CurrentAppSimulator.LicenseInformation.ProductLicenses[key].IsActive ? false : true
}
);
}
pics.ItemsSource = picItems;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
private async void ButtonBuyNow_Clicked(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string key = btn.Tag.ToString();
if (!CurrentAppSimulator.LicenseInformation.ProductLicenses[key].IsActive)
{
ListingInformation li = await CurrentAppSimulator.LoadListingInformationAsync();
string pID = li.ProductListings[key].ProductId;
string receipt = await CurrentAppSimulator.RequestProductPurchaseAsync(pID, true);
System.Diagnostics.Debug.WriteLine(receipt);
// RenderStoreItems();
}
}
I also Associate my app with Store and my app package is same as in MS Dev Center App as you can see in Image
When I run my app and click on Buy button, I got this dialogue box as you can see in Image after that I did not get receipt data from Store.
If I'm doing wrong then Please give me proper guide to implement the In-app purchase and test that In-app purchase in my laptop device.
I also had this issue and the problem was in the WindowsStoreProxy.xml file.
Solution in short
By default in the WindowsStoreProxy.xml the IsTrial is set to true and in that mode in-app purchases do not seem to work. When I changed it to false it started to work for me.
Solution a little bit longer
So first of all here we are talking about the simulation of an In-App Purchase in development time (by using the CurrentAppSimulator class). In that case you need a WindowsStoreProxy.xml file. It’s described here
Now the window you showed is opened by the CurrentAppSimulator.RequestProductPurchaseAsync line. It basically controls the return value of a Windows Runtime native method (which is very strange for me… I think it’s not intentional by Microsoft… something else should be done there), but if you let it return S_OK that basically is the case when the user paid for the in-App Purchase.
When it returns nothing then with very high probability something in the WindowsStoreProxy.xml is wrong. I suggest you to create your own WindowsStoreProxy.xml and read it with the CurrentAppSimulator.ReloadSimulatorAsync method like this:
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(#"Testing\WindowsStoreProxy.xml");
await CurrentAppSimulator.ReloadSimulatorAsync(file);
For me using the default one from C:\Users\<username>\AppData\Local\Packages\<app package folder>\LocalState\Microsoft\Windows Store\ApiData\WindowsStoreProxy.xml
did not work, but a single change already solved the problem: I changed this part
<LicenseInformation>
<App>
<IsActive>true</IsActive>
<IsTrial>true</IsTrial>
</App>
</LicenseInformation>
To this:
<LicenseInformation>
<App>
<IsActive>true</IsActive>
<IsTrial>false</IsTrial>
</App>
</LicenseInformation>
(So IsTrial was set to false...)
Now at this point I also would like to mention that this was a little bit strange, since in in the default WindowsStoreProxy.xml there was no Product defined for my In-App Purchase. So for my “RemoveAds” a proper WindowsStoreProxy.xml would be something like this:
<?xml version="1.0" encoding="utf-16" ?>
<CurrentApp>
<ListingInformation>
<App>
<AppId>00000000-0000-0000-0000-000000000000</AppId>
<LinkUri>http://apps.microsoft.com/webpdp/app/00000000-0000-0000-0000-000000000000</LinkUri>
<CurrentMarket>en-US</CurrentMarket>
<AgeRating>3</AgeRating>
<MarketData xml:lang="en-US">
<Name>AppName</Name>
<Description>AppDescription</Description>
<Price>1.00</Price>
<CurrencySymbol>$</CurrencySymbol>
<CurrencyCode>USD</CurrencyCode>
</MarketData>
</App>
<Product ProductId="RemoveAds" LicenseDuration="1" ProductType="Durable">
<MarketData xml:lang="en-US">
<Name>RemoveAds</Name>
<Price>1.00</Price>
<CurrencySymbol>$</CurrencySymbol>
<CurrencyCode>USD</CurrencyCode>
</MarketData>
</Product>
</ListingInformation>
<LicenseInformation>
<App>
<IsActive>true</IsActive>
<IsTrial>false</IsTrial>
</App>
<Product ProductId="1">
<IsActive>true</IsActive>
</Product>
</LicenseInformation>
<ConsumableInformation>
<Product ProductId="RemoveAds" TransactionId="10000000-0000-0000-0000-000000000000" Status="Active" />
</ConsumableInformation>
</CurrentApp>
Another thing I would like to point out is that the CurrentAppSimulator.RequestProductPurchaseAsync with two parameter is obsolete. Leave the true parameter out and you get PurchaseResults instance as the result, which contains the receipt in the ReceiptXML property.
WindowsStoreProxy.xml to c# code and serialize to xml file
public static CurrentApp LoadCurrentApp(string productKey = "Premium", bool isActive = false, bool isTrial = false)
{
CurrentApp currentApp = new CurrentApp();
currentApp.ListingInformation = new ListingInformation()
{
App = new App()
{
AgeRating = "3",
AppId = BasicAppInfo.AppId,
CurrentMarket = "en-us",
LinkUri = "",
MarketData = new MarketData()
{
Name = "In-app purchases",
Description = "AppDescription",
Price = "5.99",
CurrencySymbol = "$",
CurrencyCode = "USD",
}
},
Product = new Product()
{
ProductId = productKey,
MarketData = new MarketData()
{
Lang = "en-us",
Name = productKey,
Description = "AppDescription",
Price = "5.99",
CurrencySymbol = "$",
CurrencyCode = "USD",
}
}
};
currentApp.LicenseInformation = new LicenseInformation()
{
App = new App()
{
IsActive = isActive.ToString(),
IsTrial = isTrial.ToString(),
},
Product = new Product()
{
ProductId = productKey,
IsActive = isActive.ToString(),
}
};
return currentApp;
}
Base xml model
[XmlRoot(ElementName = "MarketData")]
public class MarketData
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Description")]
public string Description { get; set; }
[XmlElement(ElementName = "Price")]
public string Price { get; set; }
[XmlElement(ElementName = "CurrencySymbol")]
public string CurrencySymbol { get; set; }
[XmlElement(ElementName = "CurrencyCode")]
public string CurrencyCode { get; set; }
[XmlAttribute(AttributeName = "lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Lang { get; set; }
}
[XmlRoot(ElementName = "App")]
public class App
{
[XmlElement(ElementName = "AppId")]
public string AppId { get; set; }
[XmlElement(ElementName = "LinkUri")]
public string LinkUri { get; set; }
[XmlElement(ElementName = "CurrentMarket")]
public string CurrentMarket { get; set; }
[XmlElement(ElementName = "AgeRating")]
public string AgeRating { get; set; }
[XmlElement(ElementName = "MarketData")]
public MarketData MarketData { get; set; }
[XmlElement(ElementName = "IsActive")]
public string IsActive { get; set; }
[XmlElement(ElementName = "IsTrial")]
public string IsTrial { get; set; }
}
[XmlRoot(ElementName = "Product")]
public class Product
{
[XmlElement(ElementName = "MarketData")]
public MarketData MarketData { get; set; }
[XmlAttribute(AttributeName = "ProductId")]
public string ProductId { get; set; }
[XmlElement(ElementName = "IsActive")]
public string IsActive { get; set; }
}
[XmlRoot(ElementName = "ListingInformation")]
public class ListingInformation
{
[XmlElement(ElementName = "App")]
public App App { get; set; }
[XmlElement(ElementName = "Product")]
public Product Product { get; set; }
}
[XmlRoot(ElementName = "LicenseInformation")]
public class LicenseInformation
{
[XmlElement(ElementName = "App")]
public App App { get; set; }
[XmlElement(ElementName = "Product")]
public Product Product { get; set; }
}
[XmlRoot(ElementName = "CurrentApp")]
public class CurrentApp
{
[XmlElement(ElementName = "ListingInformation")]
public ListingInformation ListingInformation { get; set; }
[XmlElement(ElementName = "LicenseInformation")]
public LicenseInformation LicenseInformation { get; set; }
}
Get XmlFile
public async static Task<StorageFile> GetWindowsStoreProxyXmlAsync(string productKey, bool isActive = false, bool isTrial = false)
{
StorageFile xmlFile = null;
var currentApp = LoadCurrentApp(productKey, isActive, isTrial);
var xml = StorageHelper.SerializeToXML<CurrentApp>(currentApp);
if (!string.IsNullOrEmpty(xml))
{
xmlFile = await StorageHelper.LocalFolder.CreateFileAsync("MarketData.xml", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(xmlFile, xml);
}
return xmlFile;
}

Extracting detail records of XML via C# LINQ

I have the following XML excerpt. I have no problem extracting the first step of XML but I can't figure out how to get to the second layer and extract each layer. Specifically, the user info in XML below.
Any help will be appreciated...
<?xml version="1.0" encoding="UTF-8" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getStatusReportResponse xmlns:ns2="http://xxxxxxxxx.com/">
<return>
<statusReport>
<record>
<assessorDate />
<assessorOffice />
<availableDate />
<awardDate>01/01/2014</awardDate>
<awardValue>1000000</awardValue>
<businessSector>SYSTEMS</businessSector>
<user>
<accessGrantedDate />
<emailAddress>john.usda#noemail.mil</emailAddress>
<name>JOHN USDA</name>
<phoneNumber>XXX-XXX-XXXX</phoneNumber>
<role>Focal Point</role>
</user>
<user>
<accessGrantedDate />
<emailAddress>john.usda#noemail.mil</emailAddress>
<name>JOHN USDA</name>
<phoneNumber>XXX-XXX-XXXX</phoneNumber>
<role>Focal Point</role>
</user>
</record>
</statusReport>
</return>
</ns2:getStatusReportResponse>
</S:Body>
</S:Envelope>
I've tried this but it only get's me a list of the first user record and not all of them.
var records = from x in xml.Descendants("record")
select new
{
awardDate = (string) x.Descendants("awardDate").FirstOrDefault().Value
,userList = (List<string>) x.Descendants("user").Elements() //.Elements("accessGrantedDate")
.Select(a => a.Value).ToList()
};
I am assuming this is the model of your User class:
public class User
{
public DateTime? AccessGrantedDate { get; set; }
public string EMailAddress { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Role { get; set; }
}
And this is extracting the User class from the XML:
var records = from x in xml.Descendants("record")
select new
{
AwardDate = (string)x.Element("awardDate"),
UserList = x.Descendants("user").Select(user => new User
{
AccessGrantedDate = string.IsNullOrEmpty((string)user.Element("accessGrantedDate")) ?
(DateTime?) null : DateTime.Parse((string)user.Element("accessGrantedDate")),
EMailAddress = (string)user.Element("emailAddress"),
Name = (string)user.Element("name"),
PhoneNumber = (string)user.Element("phoneNumber"),
Role = (string)user.Element("role")
})
};
As far as I can tell, your code gets the data of all users in the file, but only the values of nodes.
The more organized way to do this would be:
class User
{
public string accessGrantedDate { get; set; }
public string emailAddress { get; set; }
public string name { get; set; }
public string phoneNumber { get; set; }
public string role { get; set; }
}
then:
var records = from x in xml.Descendants("record")
select new
{
awardDate = (string) x.Descendants("awardDate").FirstOrDefault().Value
,userList = x.Descendants("user").Select(a=>new User
{
accessGrantedDate= a.Element("accessGrantedDate").Value,
emailAddress=a.Element("emailAddress").Value,
name=a.Element("name").Value,
phoneNumber=a.Element("phoneNumber").Value,
role = a.Element("role").Value
}).ToList()
};
or, if you don't want to organize your data this way, you may use a list of lists:
var records = from x in xml.Descendants("record")
select new
{
awardDate = (string)x.Descendants("awardDate").FirstOrDefault().Value,
userList = x.Descendants("user").Select(a => a.Elements().Select(b=>b.Value).ToList()).ToList()
};
I solved it by using a 2nd query for detail records, therefore, creating a List as a object inside the outer list.
var records = from x in xml.Descendants("record")
select new
{
awardDate = x.Element("awardDate")
,
userList = from u in x.Descendants("user")
select new
{
accessGrantedDate = (string) u.Element("accessGrantedDate")
,emailAddress =(string) u.Element("emailAddress"
,name = (string) u.Element("name")
,phoneNumber = (string) u.Element("phoneNumber")
,role = (string) u.Element("role")
}
};

XML Parsing to c# objects

I am trying to do a XML parser which will extract data from a website using REST service, the protocol for communication is HTTP, the data I get is in XML format, and I get to the data I need after several requests to different addresses on the server. I need to parse this data to c# objects so I can operate with them lately.
The information on the server is on 5 levels( I am willing to make work only 4 of them for know)
1-List of vendors
2-List of groups
3-List of subgroups
4-List of products
5-List of full information about products
After I get to 4th level I need to check if the product is in my database or if it has different details so I can add or update it.
With "GET" request to a server I get XML with this structure:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vendors>
<vendor>
<id>someID</id>
<name>someName</name>
</vendor>
<vendor>
<id>someId1</id>
<name>somename1</name>
</vendor>
</vendors>
XML structure for groups is the same :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<groups vendor_id="43153185318">
<group>
<id>someID</id>
<name>someName</name>
</group>
<group>
<id>someId1</id>
<name>somename1</name>
</group>
The XML structure is analogical for subgroups and products, except that for products I have more elements like catalog_num, price etc.
I made the classes as follows :
public class VendorList
{
public List<Vendor> vendor_list { get; set; }
public VendorList()
{
vendor_list = new List<Vendor>();
}
}
public class Vendor
{
public string id { get; set; }
public string name { get; set; }
public List<Group> groups_list { get; set; }
public Vendor()
{
id = "N/A";
name = "N/A";
groups_list = new List<Group>();
}
}
public class Group
{
public string id { get; set; }
public string name { get; set; }
public List<SubGroup> subgroup_list { get; set; }
public Group()
{
id = "N/A";
name = "N/A";
subgroup_list = new List<SubGroup>();
}
}
public class SubGroup
{
public string id { get; set; }
public string name { get; set; }
public List<Product> product_list { get; set; }
public SubGroup()
{
id = "N/A";
name = "N/A";
product_list = new List<Product>();
}
}
public class Product
{
public string available { get; set; }
public string catalog_num { get; set; }
public string code { get; set; }
public string currency { get; set; }
public string description { get; set; }
public string haracteristics { get; set; }
public string product_id { get; set; }
public string model { get; set; }
public string name { get; set; }
public string price { get; set; }
public string price_dds { get; set; }
public string picture_url { get; set; }
public Product()
{
available = "N/A";
catalog_num = "N/A";
code = "N/A";
currency = "N/A";
description = "N/A";
haracteristics = "N/A";
product_id = "N/A";
model = "N/A";
name = "N/A";
price = "N/A";
price_dds = "N/A";
picture_url = "N/A";
}
}
and the Parser method like this :
public static void FillVendor(string url)
{
string result = GetXMLstream(url);
var vendors = new VendorList();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba.xml");
XDocument d = XDocument.Load(#"D:/proba/proba.xml");
vendors.vendor_list = (from c in d.Descendants("vendor")
select new Vendor()
{
id = c.Element("id").Value,
name = c.Element("name").Value
}).ToList<Vendor>();
foreach (Vendor v in vendors.vendor_list)
{
FillGroups(v.id);
}
}
public static void FillGroups(string vendorID)
{
string url = "main address" + vendorID;
string result = GetXMLstream(url);
var group = new Vendor();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba1.xml");
XDocument d = XDocument.Load(#"D:/proba/proba1.xml");
group.groups_list = (from g in d.Descendants("group")
select new Group()
{
id = g.Element("id").Value,
name = g.Element("name").Value
}).ToList<Group>();
foreach (Group g in group.groups_list)
{
FillSubGroup(vendorID, g.id);
}
}
public static void FillSubGroup(string vendorID, string groupID)
{
string url = "main address" + vendorID+"/"+groupID;
string result = GetXMLstream(url);
var subgroup = new Group();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba2.xml");
XDocument d = XDocument.Load(#"D:/proba/proba2.xml");
subgroup.subgroup_list = (from g in d.Descendants("subgroup")
select new SubGroup()
{
id = g.Element("id").Value,
name = g.Element("name").Value
}).ToList<SubGroup>();
foreach (SubGroup sb in subgroup.subgroup_list)
{
FillProduct(vendorID, groupID, sb.id);
}
}
public static void FillProduct(string vendorID,string groupID,string subgroupID)
{
string url = "main address" + vendorID + "/" + groupID+"/"+subgroupID;
string result = GetXMLstream(url);
var product = new SubGroup();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba2.xml");
XDocument d = XDocument.Load(#"D:/proba/proba2.xml");
product.product_list = (from g in d.Descendants("subgroup")
select new Product()
{
available = g.Element("available").Value,
catalog_num = g.Element("catalog_num").Value,
code = g.Element("code").Value,
currency = g.Element("currency").Value,
description = g.Element("description").Value,
haracteristics = g.Element("haracteristics").Value,
product_id = g.Element("id").Value,
model = g.Element("model").Value,
name = g.Element("name").Value,
price = g.Element("price").Value,
price_dds = g.Element("price_dds").Value,
picture_url = g.Element("small_picture").Value,
}).ToList<Product>();
}
But after finishing parsing I try to check if my Lists are populated with objects, but I get an error which says that they are null "NullReferenceException"
So my question is did I make classes properly and is my parsing method right ( you can suggest how to parse the xml without creating a file on my computer) and if I didn't where is my mistake and how should I make it work properly?
Thanks in advance!
modify this line add 's'( vendor -> vendors)
-> vendors.vendor_list = (from c in d.Descendants("vendor")
and the same case for group -> groups
Instead of making the classes yourself.
Create a properly formatted XML Schema either manually or with Visual Studio and then from that XSD File you've created generate your C# Classes.

Categories

Resources