Linking 4 List Boxes - c#

First off im very new to C# im trying to recreate a application i created in Java.
I have 4 Listboxes. Each box will hold a list of values from the xml file.
listBox_year for the <Year>.
listBox_make for the <Make>.
listBox_model for the <Model>.
listBox_subModel is for the <sub-Model>.
So lets say i add all the years to the listBox_year with no duplicate years. Say i click on a year it will bring up all the Make of cars that have that year. Then i click on the Make and it will bring up the models for that Make thats under that Year etc...
with Java i was able to use a HashMap to make this work to where i can have multiple keys of the same name and i can search for what ever key in this case year is selected grab all the Makes or Values that has that year as a key.
Here is the XML format
<?xml version="1.0" encoding="utf-8" ?>
<vehicles>
<Manufacturer>
<Make>Subaru</Make>
<Year>2010</Year>
<Model>Impreza</Model>
<Sub-Model>2.0i</Sub-Model>
<Highway>36 MPG highway</Highway>
<City>27 MPG city</City>
<Price>$17,495</Price>
<Description>
Symmetrical All-Wheel Drive.
SUBARU BOXER® engine.
Seven airbags standard.
>Vehicle Dynamics Control (VDC).
</Description>
</Manufacturer>
<Manufacturer>
<Make>Toyota</Make>
<Year>2012</Year>
<Model>Supra</Model>
<Sub-Model>TT</Sub-Model>
<Highway>22 MPG highway</Highway>
<City>19 MPG city</City>
<Price>$48,795</Price>
<Description>
16-inch aluminum-alloy wheels.
6-speaker audio system w/iPod® control.
Bluetooth® hands-free phone and audio.
Available power moonroof.
</Description>
</Manufacturer>
<Manufacturer>
<Make>Subaru</Make>
<Year>2011</Year>
<Model>Impreza</Model>
<Sub-Model>2.0i Limited</Sub-Model>
<Highway>36 MPG highway</Highway>
<City>27 MPG city</City>
<Price>$18,795</Price>
<Description>
16-inch aluminum-alloy wheels.
6-speaker audio system w/iPod® control.
Bluetooth® hands-free phone and audio.
Available power moonroof.
</Description>
</Manufacturer>
<Manufacturer>
<Make>Subaru</Make>
<Year>2011</Year>
<Model>Impreza</Model>
<Sub-Model>2.0i Limited</Sub-Model>
<Highway>36 MPG highway</Highway>
<City>27 MPG city</City>
<Price>$18,795</Price>
<Description>
16-inch aluminum-alloy wheels.
6-speaker audio system w/iPod® control.
Bluetooth® hands-free phone and audio.
Available power moonroof.
</Description>
</Manufacturer>
</vehicles>

The closest type to the java hashmap is the Dictionary. Since you need to have multiple items with the same key, I would use a Dictionary<int,List<Item>>.
Here are some basic function you might need:
void AddItem(int key, Item i, Dictionary<int,List<Item>> dict)
{
if (!dict.ContainsKey(key))
{
dict.Add(i,new List<Item>());
}
dict[key].Add(i);
}
List<Item> GetList(int key)
{
if (dict.ContainsKey(key))
{
return dict[key];
}
else
{
return new List<Item>(); // can also be null
}
}

Related

how to delete root tag in xml C#

hi following XML is my input XML
<?xml version="1.0" encoding="utf-8"?>
<units xmlns="http://www.elsevier.com/xml/ani/ani" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ce="http://www.elsevier.com/xml/ani/common" xsi:schemaLocation="http://www.elsevier.com/xml/ani/ani http://www.elsevier.com/xml/ani/ani512-input-CAR.xsd">
<unit type="journal" xmlns="">
<unit-info>
<timestamp>2017-01-08T02:03:14Z</timestamp>
<order-id>12535892</order-id>
<parcel-id>none</parcel-id>
<unit-id>4756202</unit-id>
</unit-info>
<unit-content>
<bibrecord>
<item-info>
<status state="new" stage="S300" />
</item-info>
<head>
<citation-info>
<citation-type code="jo" />
<citation-language xmllang="ENG" />
<abstract-language xmllang="ENG" />
<author-keywords>
<author-keyword>Stroke</author-keyword>
<author-keyword> cerebral ischaemia</author-keyword>
<author-keyword> Neuro-protection</author-keyword>
<author-keyword> Neuro-protective agents</author-keyword>
</author-keywords>
</citation-info>
<citation-title xmllang="ENG" original="y">
<titletext>PATHOGENESIS AND NEURO-PROTECTIVE AGENTS OF STROKE</titletext>
</citation-title>
<abstracts>
<abstract>
<cepara>ABSTRACT: Stroke remains worlds second leading cause of mortality; and globally most frequent cause of long-lasting disabilities. The ischaemic pathophysiologic cascade leading to neuronal damage consists of peri-infarct depolarization, excitotoxicity, inflammation, oxidative stress, and apoptosis. Despite plethora of experimental evidences and advancement into the development of treatments, clinical treatment of acute stroke still remains challenging. Neuro-protective agents, as novel therapeutic strategy confer neuro-protection by targeting the pathophysiologic mechanism of stroke. The aim of this review is discussion of summary of the literature on stroke pathophysiology, current preclinical research findings of neuroprotective agents in stroke and possible factors that were responsible for the failure of these agents to translate in human stroke therapies.</cepara>
</abstract>
</abstracts>
<correspondence>
<person>
<ceinitials>M.</ceinitials>
<cesurname>Mubarak</cesurname>
</person>
<affiliation>
<organization> Bayero University Kano</organization>
<organization> Nigeria. Email: mubarakmahmad#yahoo.com</organization>
</affiliation>
</correspondence>
<root>
<author Seq="0">
<Inital>A.El</Inital>
<Surname>Khattabi</Surname>
<Givenname>Abdelkrim</Givenname>
</author>
</root>
</head>
</bibrecord>
</unit-content>
from this xml i need to delete root tag alone but i need all child element
<author Seq="0">
<Inital>A.El</Inital>
<Surname>Khattabi</Surname>
<Givenname>Abdelkrim</Givenname>
</author>
how to delete root tag alone. For this i followed following code
XDocument CarXML = new XDocument();
CarXML.Add(Root);
CarXML.Descendants("root").Remove();
CarXML.Save(#"CAR.XML");
But this code delete all xml tag. How to delete root element alone. i need child element
CarXML.ReplaceNodes(CarXML.Descendants("author").FirstOrDefault());
This replaces the whole content of the XML with the first descendant named author.

Compare XML for Differences

I am writing some C# code with the intention of periodically downloading an XML file, comparing the resulting XML with a previously downloaded version of XML (to detect changes), and then "actioning" the changes (via appropriate CRUD statements) by updating database records which reflect the entities in the XML.
I need some help with the "comparing the resulting XML with a previously downloaded version to detect changes" part of the requirements.
So... considering the following two XML documents which have minor differences...
Original
<ROOT>
<Stock>
<Vehicle id="2574074">
<DealerName>Super Cars London</DealerName>
<FriendlyName>Ford Ranger 3.2 double cab 4x4 XLT auto</FriendlyName>
<ModelName>Ranger</ModelName>
<MakeName>Ford</MakeName>
<Registration>DG55TPG</Registration>
<Price>40990</Price>
<Colour>WHITE</Colour>
<Year>2014</Year>
<Mileage>52000</Mileage>
<Images>
<Image Id="4771304" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=4771304&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=4771304&Type=6&Width=640&FeedId=42" LastModified="2016-02-02T08:24:51.48" Priority="1"/>
</Images>
</Vehicle>
<Vehicle id="2648665">
<DealerName>Super Cars London</DealerName>
<FriendlyName>BMW 320i</FriendlyName>
<ModelName>3 Series</ModelName>
<MakeName>BMW</MakeName>
<Registration>CN03YZG</Registration>
<Price>24990</Price>
<Colour>WHITE</Colour>
<Year>2013</Year>
<Mileage>96000</Mileage>
<Images/>
</Vehicle>
</Stock>
</ROOT>
New
<ROOT>
<Stock>
<Vehicle id="2575124">
<DealerName>Supercars London</DealerName>
<FriendlyName>Ford Ranger 3.2 double cab 4x4 XLT auto</FriendlyName>
<ModelName>Ranger</ModelName>
<MakeName>Ford</MakeName>
<Registration>DK08FKP</Registration>
<Price>43990</Price>
<Colour>WHITE</Colour>
<Year>2014</Year>
<Mileage>30000</Mileage>
<Images>
<Image Id="5119812" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5119812&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5119812&Type=6&Width=640&FeedId=42" LastModified="2016-04-11T13:08:42.81" Priority="1"/>
</Images>
</Vehicle>
<Vehicle id="2648665">
<DealerName>Super Cars London</DealerName>
<FriendlyName>BMW 320i</FriendlyName>
<ModelName>3 Series</ModelName>
<MakeName>BMW</MakeName>
<Registration>CN03YZG</Registration>
<Price>24990</Price>
<Colour>BRILLIANT WHITE</Colour>
<Year>2013</Year>
<Mileage>96000</Mileage>
<Images>
<Image Id="5201856" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201856&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201856&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:05.827" Priority="1"/>
<Image Id="5201857" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201857&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201857&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:09.117" Priority="2"/>
<Image Id="5201858" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201858&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201858&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:13.59" Priority="3"/>
<Image Id="5201859" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201859&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201859&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:18.453" Priority="4"/>
<Image Id="5201860" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201860&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201860&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:22.853" Priority="5"/>
</Images>
</Vehicle>
</Stock>
</ROOT>
Summary of Differences
Vehicle id="2575124" is not present in the original. This represents a "create".
Vehicle id="2574074" is not present in the new. This represents a "delete".
Vehicle id="2648665" (which is present in both original and new) has a different <Colour> (WHITE -> BRILLIANT WHITE). This represents an "update".
Vehicle id="2648665" also has new <Image> nodes in the new. This represents a "create" (as images will be 1:M with the vehicle in the database).
I have looked at XMLDiff to generate a DiffGram with add/change/remove instructions but I can't see a way to make it generate a DiffGram that represent the changes I've summarised, e.g. it sees changes 1 and 2 as an "change" - <xd:change match="#id">2648665</xd:change> - rather than the absence and addition of a vehicle.
Is there a way to do this with XMLDiff?
Or, is there a "better" way to achieve the result I'm looking for?
I've found that LINQ can do a pretty good job of parsing the XML for differences, e.g...
XDocument xNewVehicle = new XDocument(new XElement("UsedStock",
from newVehicle in newXml.Descendants("Vehicle")
join oldVehicle in oldXml.Descendants("Vehicle")
on newVehicle.Attributes("id").First().Value equals oldVehicle.Attributes("id").First().Value into oldVehicles
where !oldVehicles.Any() // where the vehicle exists in new but not in old
|| newVehicle.ToString() != oldVehicles.First().ToString() // where the new vehicle is not the same as the old vehicle
select newVehicle));

Web Service List Record

Below is my result from web service, how can I change the record name so that it's uniquely identified? e.g. Record1, Record2, Record3.
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfRecord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Record> /*How to change this Record to Record1*/
<item>
Buffalo DriveStation Combo 3.5 inch External Hard Drive USB 2.0 1.5TB Quadra
</item>
<qty>1</qty>
<qtyapproved>1</qtyapproved>
<availability>0</availability>
<description>Returned</description>
<itemdescription>-</itemdescription>
<id>188</id>
</Record>
<Record>
<item>eSataII HDD Docking with 1TB 3.5 inch hard disk</item>
<qty>1</qty>
<qtyapproved>1</qtyapproved>
<availability>0</availability>
<description>Returned</description>
<itemdescription/>
<id>184</id>
</Record>
</ArrayOfRecord>
Instead of changing the record name include a unique column in the resultset being returned by the web service, you can use a sequence number if you do not have any unique column.
For example you can add itemId a new column in Record
<Record> /*How to change this Record to Record1*/
<itemId>1</itemId>
<item>
Buffalo DriveStation Combo 3.5 inch External Hard Drive USB 2.0 1.5TB Quadra
</item>
<qty>1</qty>
<qtyapproved>1</qtyapproved>
<availability>0</availability>
<description>Returned</description>
<itemdescription>-</itemdescription>
<id>188</id>
</Record>

Programmatically create iTunes Podcast RSS Feed

I am trying to create an program that automatically creates iTunes Podcast RSS feeds. The problem I am running into is I dont know how to create the require XML elements. I tried to create two tags here in two different ways. First I used "itunes:" for subtitle and it doesnt work it throws an exception that I cant use the colon in my name. The second one (image) outputs this
<image xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd" href="http://someurkl.com/myimgp.png"/>
Is there any way to get this to work with ASP.net? Or can you point me the right direction of documentation or a tutorial that can show me how to create a feed for a iTunes podcast.
My Code:
XNamespace itunesNS = "http://www.itunes.com/dtds/podcast-1.0.dtd";
SyndicationFeed feed = new SyndicationFeed(title, description, new Uri(link));
feed.ElementExtensions.Add(new XElement("itunes:" + "subtitle", subTitle).CreateReader());
feed.ElementExtensions.Add(new XElement(itunesNS + "image", new XAttribute("href", imageUrl)).CreateReader());
Format Required By iTunes:
<itunes:subtitle>My Subtitle Here</itunes:subtitle>
Example Feed from Apple:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>All About Everything</title>
<link>http://www.example.com/podcasts/everything/index.html</link>
<language>en-us</language>
<copyright>℗ & © 2005 John Doe & Family</copyright>
<itunes:subtitle>A show about everything</itunes:subtitle>
<itunes:author>John Doe</itunes:author>
<itunes:summary>All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our Podcast in the iTunes Store</itunes:summary>
<description>All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our Podcast in the iTunes Store</description>
<itunes:owner>
<itunes:name>John Doe</itunes:name>
<itunes:email>john.doe#example.com</itunes:email>
</itunes:owner>
<itunes:image href="http://example.com/podcasts/everything/AllAboutEverything.jpg" />
<itunes:category text="Technology">
<itunes:category text="Gadgets"/>
</itunes:category>
<itunes:category text="TV & Film"/>
<item>
<title>Shake Shake Shake Your Spices</title>
<itunes:author>John Doe</itunes:author>
<itunes:subtitle>A short primer on table spices</itunes:subtitle>
<itunes:summary>This week we talk about salt and pepper shakers, comparing and contrasting pour rates, construction materials, and overall aesthetics. Come and join the party!</itunes:summary>
<itunes:image href="http://example.com/podcasts/everything/AllAboutEverything/Episode1.jpg" />
<enclosure url="http://example.com/podcasts/everything/AllAboutEverythingEpisode3.m4a" length="8727310" type="audio/x-m4a" />
<guid>http://example.com/podcasts/archive/aae20050615.m4a</guid>
<pubDate>Wed, 15 Jun 2005 19:00:00 GMT</pubDate>
<itunes:duration>7:04</itunes:duration>
</item>
<item>
<title>Socket Wrench Shootout</title>
<itunes:author>Jane Doe</itunes:author>
<itunes:subtitle>Comparing socket wrenches is fun!</itunes:subtitle>
<itunes:summary>This week we talk about metric vs. old english socket wrenches. Which one is better? Do you really need both? Get all of your answers here.</itunes:summary>
<itunes:image href="http://example.com/podcasts/everything/AllAboutEverything/Episode2.jpg" />
<enclosure url="http://example.com/podcasts/everything/AllAboutEverythingEpisode2.mp3" length="5650889" type="audio/mpeg" />
<guid>http://example.com/podcasts/archive/aae20050608.mp3</guid>
<pubDate>Wed, 8 Jun 2005 19:00:00 GMT</pubDate>
<itunes:duration>4:34</itunes:duration>
</item>
<item>
<title>Red, Whine, & Blue</title>
<itunes:author>Various</itunes:author>
<itunes:subtitle>Red + Blue != Purple</itunes:subtitle>
<itunes:summary>This week we talk about surviving in a Red state if you are a Blue person. Or vice versa.</itunes:summary>
<itunes:image href="http://example.com/podcasts/everything/AllAboutEverything/Episode3.jpg" />
<enclosure url="http://example.com/podcasts/everything/AllAboutEverythingEpisode1.mp3" length="4989537" type="audio/mpeg" />
<guid>http://example.com/podcasts/archive/aae20050601.mp3</guid>
<pubDate>Wed, 1 Jun 2005 19:00:00 GMT</pubDate>
<itunes:duration>3:59</itunes:duration>
</item>
</channel>
</rss>
TL;DR
Looks like you need to define a custom namespace for the itunes: tag in the area of your code that generates your rss.
Here is some info originally written by Lukasz Karolak, that might help you.
Let’s assume we would like to have a RSS feed, which in fact would serve as a podcast, e.g. for iTunes. The software from Apple uses some information from their custom-defined RSS-tags, with an itunes prefix, for example:
<itunes:author>Anonymous One</itunes:author>
Without this prefix it’s very easy. Our SyndicationItem class provides us a functionality to extend the standard item’s elements:
SyndicationItem item = new SyndicationItem();
item.ElementExtensions.Add(customTagString.Empty, "My test");
The second attribute is the namespace which comes into play in the next step. In order to add the tag prefix as mentioned before, one has start with adding the namespace to the feed instance:
SyndicationFeed feed = new SyndicationFeed();
XmlQualifiedName n=new XmlQualifiedName("itunes","http://www.w3.org/2000/xmlns/");
String itunesNs = "http://www.itunes.com/dtds/podcast-1.0.dtd";
feed.AttributeExtensions.Add(n, itunesNs);
Now that we have added the new namespace to the feed, we can start adding custom item elements within that namespace.
SyndicationItem item = new SyndicationItem();
item.ElementExtensions.Add(new SyndicationElementExtension("author",
itunesNs, "Famous author"));
That should solve the issue with custom tags with custom prefixes.

How to split and store XML in SQL Server 2008 R2 using C#?

<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
<pagination>
<itemsPerPage>{Number of Inventories per Page}</itemsPerPage>
<numberOfItems>{Number of Inventories}</numberOfItems>
</pagination>
<itemList>
<item>
{Requested Salesforce fields e.g:}
<Id>{Salesforce Id}</Id>
<Name>{Name}</Name>
<pb__IsForSale__c>{e.g.}false</pb__IsForSale__c>
<pb__IsForLease__c>{e.g.}true</pb__IsForLease__c>
<pb__ItemDescription__c>{Item Description}</pb__ItemDescription__c>
<pb__PurchaseListPrice__c>{Item List Price e.g.:}2000000.00</pb__PurchaseListPrice__c>
<CurrencyIsoCode>{Currency Iso Code e.g:}EUR</CurrencyIsoCode>
<pb__UnitBedrooms__c>{Number of Bedrooms}</pb__UnitBedrooms__c>
<asset>
<Id>{internal Propertybase InventoryAsset Id}</Id>
<category>{Images, Videos or Documents}</category>
<isExternalLink>false</isExternalLink>
<title>{title}</title>
<filename>{original name of the uploaded file}</filename>
<url>{full url to image/video/document}</url>
<thumbnailUrl>{full url to thumbnail image}</thumbnailUrl>
<midresUrl>{full url to thumbnail image}</midresUrl>
<tags>{comma separated tags}</tags>
<mimeType>{e.g. image/jpeg}</mimeType>
</asset>
<asset>
<Id>{internal Propertybase InventoryAsset Id}</Id>
<category>{Images, Videos or Documents}</category>
<isExternalLink>true</isExternalLink>
<title>{title}</title>
<url>{full url to image/video/document}</url>
<tags>{comma separated tags}</tags>
</asset>
<asset>
{...}
</asset>
{more assets ...}
</item>
<item>
{...}
</item>
{more items ...}
</itemList>
</searchResult>
Well I have the above XML from which I need to store the item tag in a database table along with its child nodes as attributes, and have to store the asset tag and its child nodes in another table with the item id as foreign key from the item table, How do I do it done a lots of googeling but can't get of a way to get it to work
You simply need to select all Item nodes.
Using XPath (with SelectNodes, for example) this would be \\Item - this will produce a node set that you can iterate over.

Categories

Resources