C# MVC: How extract values from XML and pass to view - c#

I have a C# MVC application which I need to output the following data to a view:
<versions>
<product>true</product>
<type>city</type>
<factory name="Demme" url="http://test1.com" thumbnail="http://test3.com/img1" interval="10" />
<factory name="Vollick" url="http://test2.com" thumbnail="http://test3.com/img1" interval="10" />
<factory name="Tony" url="http://test3.com" thumbnail="http://test3.com/img1" interval="10" />
</versions>
The above data comes from a SQL table/column which stores the data as a XML data type.
Can somone give me a code example to extract the values of the elements(maybe assign each value to variable) so I can pass it to a view?
So I need to get the values "true" , "City", "Demme" , "http://test1.com", "http://test3.com/img1....and so on.
Whats the best way to present this data to a view?

My idea is to create classes, corresponding to your Xml file, a Version class, a Factory class. Load the xml file, and then pass it to your class that return your data, here is how I do it :
The Version class :
public class Version
{
public bool IsProduct { get; set; }
public string City { get; set; }
public List<Factory> Factories { get; set; }
//Create a version
public Version(XElement xVersion)
{
IsProduct = Convert.ToBoolean(xVersion.Element("Product").Value);
City = xVersion.Element("City").Value;
Factories = Factory.GetFactories(xVersion);
}
//Get the list of versions
public static List<Version> GetVersions(XElement xDocument)
{
if (xDocument == null)
return null;
List<Version> list = new List<Version>();
var xVersions = xDocument.Elements("Version");
foreach (var xVersion in xVersions)
{
list.Add(new Version(xVersion));
}
return list;
}
}
The Factory class :
public class Factory
{
public string Name { get; set; }
public string Url { get; set; }
public string Thumbnail { get; set; }
public string Interval { get; set; }
//Create a factory
public Factory(XElement xFactory)
{
Name = xFactory.Attribute("Name").Value;
Url = xFactory.Attribute("Url").Value;
Thumbnail = xFactory.Attribute("Thumbnail").Value;
Interval = xFactory.Attribute("Interval").Value;
}
//Get the factories of a version
public static List<Factory> GetFactories(XElement xVersion)
{
var xFactories = xVersion.Elements("Factory");
if (xFactories == null)
return null;
List<Factory> list = new List<Factory>();
foreach (var xFactory in xFactories)
{
list.Add(new Factory(xFactory));
}
return list;
}
}
And last, in your MCV Controller :
private void myMethod()
{
var xDocument = XElement.Load("XmlFilePath");
var versions = Version.GetVersions(xDocument);
//And then, pass the -versions- to your typed view ^^
}

using System.Xml;
List<string> values= new List<string>();
XmlTextReader reader = new XmlTextReader ("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
while (reader.MoveToNextAttribute()) // Read the attributes.
values.add(reader.Value);
break;
case XmlNodeType.Text: //Display the text in each element.
values.add(reader.Value);
break;
case XmlNodeType. EndElement: //Display the end of the element.
Console.WriteLine(">");
break;
}
}
Now you have a list of values. Assign it to the model and then use the model to populate the view.

Related

C# Index was outside the bounds of the array while generating Json objects

I have created a class:
internal class Movie
{
public class BaseResponse
{
public Item[] search { get; set; }
public string response { get; set; }
}
public class Item
{
public string title { get; set; }
}
I want to create n objects for search array like this:
public void Generate()
{
Movie.BaseResponse baseResponse = new Movie.BaseResponse();
baseResponse.response = "True!";
baseResponse.search = new Movie.Item[] { new Movie.Item()};
baseResponse.search[0].title = "Title one";
baseResponse.search[1].title = "Title two"; //Error accurs here****
string response = JsonConvert.SerializeObject(baseResponse);
}
but this script does not work right and it gives the following error:
Index was outside the bounds of the array
Can anyone explain how can I create n objects of search array in the Movie class?
Rather than using an array (T[]), you may be better off using a List<T>. You need to know how many items will be in an array up front (since they get allocated contiguously). You can add new items to a List at any time.
Changing your code:
internal class Movie
{
public class BaseResponse
{
public List<Item> Search { get; set; } = new List<Item>();
public string Response { get; set; }
}
public class Item
{
public string Title { get; set; }
}
}
public void Generate()
{
Movie.BaseResponse baseResponse = new Movie.BaseResponse();
baseResponse.Response = "True!";
baseResponse.Search.Add (new Movie.Item { Title = "Title One" });
baseResponse.Search.Add (new Movie.Item { Title = "Title Two" });
string response = JsonConvert.SerializeObject(baseResponse);
}
That seems to work. I get this as response:
{
"Search":[
{"Title":"Title One"},
{"Title":"Title Two"}],
"Response":"True!"
}
You've create an array with 1 item, but you're trying to access the non-existent second item.
Change the relevant line to something like:
baseResponse.search = new Movie.Item[] { new Movie.Item(), new Movie.Item() };

C# Add List/Lists to Data Class instance

Very new to C# and I'm trying to create a list of node data which contains a variable list length of Link data.
class Data
{
public List<Node> Node { get; set; }
}
public class Node
{
public string viewer { get; set; }
public int viewerId { get; set; }
public string log { get; set; }
public List <Link> Link { get; set; }
}
public class Link
{
public string keyName { get; set; }
public int value { get; set; }
}
i have a for loop iterating through the configured nodes and an inner for loop
to grab any configured links.
Data data = new Data();
data.Node = new List<Node>();
I'm doing the following for each new node, which is working how i want it.
data.Node.Add( new Node {
viewer = setup.Device[moduleNr].viewer,
viewerId = setup.Device[moduleNr].viewerId ,
log = setup.Device[moduleNr].log
// how to add one or more lists of Link to this list???
});
The problem i'm having is adding a new list/lists inside the existing data.Node???
Ultimately i would like to achieve the following -
data
|->Node
|->[0]
|->Link
|->[0]
|->keyname
|->value
|->[1]
|->keyname
|->value
|->[2]
|->keyname
|->value
|->log
|->viewerId
|->viewer
|->[1]
|->Link
|->[0]
|->keyname
|->value
|->[1]
|->keyname
|->value
|->log
|->viewerId
|->viewer
|->[2]
|->Link
|->[0]
|->keyname
|->value
|->log
|->viewerId
|->viewer
Would really appreciate some help with this issue - Thanks
You can add a new instance of a List<Link> like this and use the constructor to add new items:
data.Node.Add(new Node {
viewer = setup.Device[moduleNr].viewer,
viewerId = setup.Device[moduleNr].viewerId ,
log = setup.Device[moduleNr].log,
Link = new List<Link>
{
new Link
{
keyName = "Link 1",
value = 0
},
new Link
{
keyName = "Link 2",
value = 1
}
}
});

Displaying self-referencing table's data in a c# application

I have following table:
---------------------
Id Title Parent
---------------------
1 Parent NULL
2 Level_1 1
3 Level_2 1
4 Level_3 1
5 Level NULL
6 Level_New 5
Now I want to display these data in my console application, I know I need a recursive function but no idea how to do it becuase I want to read these data using ADO.NET not EntityFramework.In EF I could define a model that has a navigation property for children:
public class Menu
{
public int Id { get; set; }
public string Title { get; set; }
public int? Parent { get; set; }
public ICollection<Menu> Children { get; set; }
}
But the problem is that I don't want to use EF. I want to do it using raw ADO.NET
Recursion isn't fun, this is a solution that I used to test for a much larger recursion
public class MyObject
{
public string Id;
public string ParentId;
public string Name;
public string Comments;
}
a lot of this code you wont need, but this should give you want you need on recursion.
private void BindTree(IEnumerable<MyObject> list, TreeNode parentNode, string previousNode)
{
var myObjects = list as IList<MyObject> ?? list.ToList();
var nodes = myObjects.Where(x => (parentNode == null ? x.ParentId == "[].[].[(root)]" : x.ParentId == parentNode.Value));
var listOfNodeNames = new List<string>();
foreach (var node in nodes)
{
var newNode = new TreeNode(node.Name, node.Id);
BindTree(myObjects, newNode, previousNode);
}
}
The above code does the recursion I need ( code you wont need stripped out ) and builds a treeview on a page based on data from a datatable.
But, this should give you want you need to do your recursion.
You need to pull data from server first, then construct tree on client side. Beware of circular reference.
First, change your Menu class to ensure that Children will never null
public class Menu
{
public Menu()
{
Children = new HashSet<Menu>();
}
public int Id { get; set; }
public string Title { get; set; }
public int? Parent { get; set; }
public ICollection<Menu> Children { get; private set; }
}
Then pull the data from database, and construct the tree
var connBuilder = new SqlConnectionStringBuilder();
connBuilder.DataSource = "localhost";
connBuilder.InitialCatalog = "YourDatabaseName";
connBuilder.IntegratedSecurity = true;
using (var con = new SqlConnection(connBuilder.ToString()))
{
con.Open();
var list = new List<Menu>();
//pull data from database
using (var cmd = con.CreateCommand())
{
cmd.CommandText = "SELECT Id, Title, Parent FROM [dbo].[YourTableName]";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(new Menu
{
Id = reader.GetInt32(0),
Title = reader.GetString(1),
Parent = reader.IsDBNull(2) ?(int?) null : reader.GetInt32(2)
});
}
}
}
//construct tree
var newList = new List<Menu>();
foreach (var l1 in list)
{
if (l1.Parent == null)
{
newList.Add(l1);
}
foreach (var l2 in list)
{
if (l2.Parent == l1.Id)
{
l1.Children.Add(l2);
}
}
}
// do whatever you want with newList
}
You will get data like this

Updating Custom Class in List<T>

I am trying to update a List which is a List of Interfaces to concrete classes.
I add to the List each Market type i am interested in, for this Example these Markets are A and B
I loop over all the markets, (sample provided with 3 markets A B & C, we are only interested in A and B) And determine which is of interest to us.
Once found we pass this to an extraction method too do its work and create an instance of the Correct Market_ class type.
This all works fine, but when i try to update the list with the Updates it does not get reflected in the List.
Code below, any Suggestions?
Thanks
public class Test
{
public Test()
{
TheMarkets MarketsToUpdate = new TheMarkets();
List<SpecificCompanyMarket> lstMarks = new List<SpecificCompanyMarket>();
lstMarks.Add(new SpecificCompanyMarket(1234, "A", "Some HTML DATA HERE"));
lstMarks.Add(new SpecificCompanyMarket(5874, "B", "Some HTML DATA HERE"));
lstMarks.Add(new SpecificCompanyMarket(2224, "C", "Some HTML DATA HERE"));
foreach (var item in lstMarks)
{
if (MarketsToUpdate.IsMarketWeAreInterestedIn(item.MarketName))
{
ITheMarkets MarkToUpdate = ExtractMarketData(item);
var obj = MarketsToUpdate.MarketsWeAreInterestedIn.FirstOrDefault(x => x.MarketName() == "A");
if (obj != null)
{
obj = MarkToUpdate;
}
}
}
//Look At MarketsToUpdate Now and the item has not changed, still original values
//I was expecting to see the new values for the fields in A, not the default 0's
}
public ITheMarkets ExtractMarketData(SpecificCompanyMarket item)
{
ITheMarkets market = null;
if (item.MarketName.ToUpper() == "A")
{
Market_A marketType = new Market_A();
marketType.SomeValue1 = 123;
marketType.SomeValue2 = 158253;
market = marketType;
}
//Other Market extractions here
return market;
}
}
public class SpecificCompanyMarket
{
public int MarketId { get; set; }
public string MarketName { get; set; }
public string MarketDataHTML { get; set; }
public SpecificCompanyMarket(int MID, string MName, string MData)
{
MarketId = MID;
MarketName = MName;
MarketDataHTML = MData;
}
}
public class TheMarkets
{
public List<ITheMarkets> MarketsWeAreInterestedIn = new List<ITheMarkets>();
public TheMarkets()
{
Market_A A = new Market_A();
Market_B B = new Market_B();
MarketsWeAreInterestedIn.Add(A);
MarketsWeAreInterestedIn.Add(B);
}
public bool IsMarketWeAreInterestedIn(string strMarketName)
{
bool blnRetVal = false;
foreach (var item in MarketsWeAreInterestedIn)
{
if (item.MarketName().ToUpper().Trim().Equals(strMarketName.ToUpper().Trim()))
{
blnRetVal = true;
break;
}
}
return blnRetVal;
}
}
public interface ITheMarkets
{
string MarketName();
}
public class Market_A : ITheMarkets
{
public string LabelType { get; private set; }
public double SomeValue1 { get; set; }
public double SomeValue2 { get; set; }
public double SomeValue3 { get; set; }
public Market_A()
{
LabelType = "A";
}
public string MarketName()
{
return LabelType;
}
}
public class Market_B : ITheMarkets
{
public string LabelType { get; private set; }
public List<string> SomeList { get; set; }
public double SomeValue { get; set; }
public Market_B()
{
LabelType = "B";
}
public string MarketName()
{
return LabelType;
}
}
This is a short example to get you going. Loop through your list, find the object you want to update, create a new object of that type and then find the original objects index in the list and overwrite it in place. You are essentially just replacing the object in the list with a new one not mutating the existing one.
foreach (var item in lstMarks)
{
//your code to get an object with data to update
var yourObjectToUpdate = item.GetTheOneYouWant();
//make updates
yourObjectToUpdate.SomeProperty = "New Value";
int index = lstMarks.IndexOf(item);
lstMarks[index] = yourObjectToUpdate;
}
You are extracting an obj from marketWeAreInterestedIn list using LINQ's firstOrDefault extension. This is a new object and not a reference to the obj in that list. Therefore, no updates will be reflected in the object inside that list. Try using 'indexof'
You are not storing "list of interfaces" in your list. List<T> stores an array of pointers to objects that support T interface. Once you enumerate (with Linq in your case) your list, you copy a pointer from list, which is not associated with list itself in any way. It is just a pointer to your instance.
To do what you want, you will have to build new list while enumerating the original one, adding objects to it, according to your needs, so the second list will be based on the first one but with changes applied that you need.
You can also replace specific instance at specific index instead of building new list in your code, but to do this you will need to enumerate your list with for loop and know an index for each item:
list[index] = newvalue;
But there is a third solution to update list item directly by Proxying them. This is an example
class ItemProxy : T { public T Value { get; set; } }
var list = new List<ItemProxy<MyClass>>();
list.Insert(new ItemProxy { Value = new MyClass() });
list.Insert(new ItemProxy { Value = new MyClass() });
list.Insert(new ItemProxy { Value = new MyClass() });
foreach(var item in list)
if(item // ...)
item.Value = new MyClass(); // done, pointer in the list is updated.
Third is the best case for perfomance, but it will be better to use this proxying class for something more than just proxying.

How to Create multi level Json using Jobject in C#?

I want to create multi level Json, Using http://json2csharp.com/. I created classes. But not sure how to use it.
public class MassPay
{
public string legal_name { get; set; }
public string account_number { get; set; }
public string routing_number { get; set; }
public string amount { get; set; }
public string trans_type { get; set; }
public string account_class { get; set; }
public string account_type { get; set; }
public string status_url { get; set; }
public string supp_id { get; set; }
public string user_info { get; set; }
}
public class MassPayList
{
public string oauth_consumer_key { get; set; }
public string bank_id { get; set; }
public string facilitator_fee { get; set; }
public IList<MassPay> mass_pays { get; set; }
}
These are my classes and this is Json Format i want to create...
there are extra elements...
{
"oauth_consumer_key":"some_oauth_token",
"mass_pays":[
{"legal_name":"SomePerson1",
"account_number":"888888888",
"routing_number":"222222222",
"amount":"10.33",
"trans_type":"0",
"account_class":"1",
"account_type":"2"
},
{"legal_name":"SomePerson2",
"account_number":"888888888",
"routing_number":"222222222",
"amount":"10.33",
"trans_type":"0",
"account_class":"1",
"account_type":"1"}
]
}
So far i have come up with below code..I am using JObject, and all others wer single level so it was pretty easy. but when it comes to two or three level its difficult.
public JObject AddMassPayRequest(MassPayList lMassPayList, MassPay lMassPay)
{
JObject pin = new JObject(
new JProperty("legal_name", lMassPay.legal_name),
new JProperty("account_number", lMassPay.account_number),
new JProperty("routing_number", lMassPay.routing_number),
new JProperty("amount", lMassPay.amount),
new JProperty("trans_type", lMassPay.trans_type),
new JProperty("account_class", lMassPay.account_class),
new JProperty("account_type", lMassPay.account_type),
new JProperty("status_url", lMassPay.status_url),
new JProperty("supp_id", lMassPay.supp_id),
new JProperty("status_url", lMassPay.status_url),
new JProperty("user_info", lMassPay.user_info)
);
return pin;
}
public JObject AddMassPayRequestList(MassPayList lMassPayList, MassPay lMassPay)
{
JObject pin = new JObject(
new JProperty("mass_pays", lMassPayList.mass_pays),
new JProperty("bank_id", lMassPayList.bank_id),
new JProperty("facilitator_fee", lMassPayList.facilitator_fee),
new JProperty("oauth_consumer_key", lMassPayList.oauth_consumer_key)
);
return pin;
}
Can some one help me how to do this..?
if you're using ASP.NET MVC you just need to use the Json response action using your existing classes.
You could simply do something like this in a controller:
return Json(new { PoId = newPoId, Success = true });
or an actual concrete model class:
var _AddMassPayRequestList = new AddMassPayRequestList();
...
returning a populated instance of your AddMassPayRequestList class:
return Json(_AddMassPayRequestList);
So finally I got this answer, Its simple structure. Using this u can create any type of Json... It doesnt have to follow same structure..
The logic behind this is add things you want at start, create class and inside that properties you want to add into json. SO while passign just add for loop and pass Object to the list.. It will loop through and create JSon for You..
If you have any doubts, let me know happy to help you
public String ToJSONRepresentation(List<MassPay> lMassPay)
{
StringBuilder sb = new StringBuilder();
JsonWriter jw = new JsonTextWriter(new StringWriter(sb));
jw.Formatting = Formatting.Indented;
jw.WriteStartObject();
jw.WritePropertyName("oauth_consumer_key");
jw.WriteValue("asdasdsadasdas");
jw.WritePropertyName("mass_pays");
jw.WriteStartArray();
int i;
i = 0;
for (i = 0; i < lMassPay.Count; i++)
{
jw.WriteStartObject();
jw.WritePropertyName("legal_name");
jw.WriteValue(lMassPay[i].legal_name);
jw.WritePropertyName("account_number");
jw.WriteValue(lMassPay[i].account_number);
jw.WritePropertyName("routing_number");
jw.WriteValue(lMassPay[i].routing_number);
jw.WritePropertyName("amount");
jw.WriteValue(lMassPay[i].amount);
jw.WritePropertyName("trans_type");
jw.WriteValue(lMassPay[i].trans_type);
jw.WritePropertyName("account_class");
jw.WriteValue(lMassPay[i].account_class);
jw.WritePropertyName("account_type");
jw.WriteValue(lMassPay[i].account_type);
jw.WritePropertyName("status_url");
jw.WriteValue(lMassPay[i].status_url);
jw.WritePropertyName("supp_id");
jw.WriteValue(lMassPay[i].supp_id);
jw.WriteEndObject();
}
jw.WriteEndArray();
jw.WriteEndObject();
return sb.ToString();
}

Categories

Resources