Getting Just the ValueMember from ComboBox - c#

I have a combobox with I am filling like this:
comboBox1.DataSource = items;
comboBox1.DisplayMember = "Item";
comboBox1.ValueMember = "Value";
Later I am attempting to get the value like this:
string value = comboBox1.SelectedValue.ToString();
I am expecting a string which contains "DFT". What I am getting, however is a string that contains "{ Item = Default Project, Value = DFT }".
Any idea how I can get just the DFT out of there?
Thanks
EDIT: Forgot that it wasn't apparent. Items is a List generated from an XDocument (Linq) using the descendants method. The XML looks like:
<Parent>
<Item>
<Name>Default project</Name>
<Value>DFT</Value>
</Item>
</Parent>
EDIT 2: This is my best guess as to the method I used to generate list (its at work and I don't remember it fully):
var items = (from i in xmlDoc.Descendants("item")
select new { Item = i.Element("Name").Value, Value = i.Element("Value").Value }).ToList();

Related

Displaying XML data selected from Combobox in Textbox C#

Hello I'm learning C# and I'm trying to show xml information from within User1.xml in a Textbox on the same form after it has been selected in the combobox.
I have managed to populate the combobox with the name tag using this code. (So that a username may be selected)
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument sFor = new XmlDocument();
sFor.Load(Path.GetFullPath("User1.xml"));
XmlNodeList SearchList = sFor.SelectNodes("employee/user/name");
foreach (XmlNode Search in SearchList)
{
comboBox1.Items.Add(Search.InnerText);
}
}
The XML is formatted thusly
<employee>
<user>
<name>John Smith</name>
<department>PAI</department>
<manager>MD</manager>
<hours>full-time</hours>
<leave>940</leave>
</user>
</employee>
How would I (Using linq or xmlreader or otherwise) after selection in the Combobox, display the information in textBox2?
Thank you.
I did something similar with a DataGridView looking for a "City". The input field was Textbox1.Text. I hope this isnt overkill. The credit goes to the posters here at Stack for showing me how to do this! I pulled the specific data first, then load it to a List. lastly, made the List the data source for DGV. That might work for a combo box...yes?
<TaxTbl>
<TaxSite>
<Location>Axx</Location>
<Address>xxx</Address>
<City>aaa</City>
<State> st</State>
<Zip>xxx</Zip>
</TaxSite>
<TaxSite>
<Location>Bxxx</Location>
<Address>xxx</Address>
<City>xxx</City>
<State> st</State>
<Zip>xxx</Zip>
</TaxSite>
</TaxTbl>
var xdoc = XDocument.Load("C:\\Users\\Harley\\desktop\\outfile.xml");
var NewDoc = new XDocument(new XElement("TaxTbl",
from anEntry in xdoc.Element("TaxTbl").Elements("TaxSite")
where anEntry.Element("City").Value.Contains(textBox1.Text)
select anEntry));
var MyList =
(from bEntry in NewDoc.Descendants("TaxSite")
select new
{
Location = bEntry.Element("Location").Value,
Address = bEntry.Element("Address").Value,
City = bEntry.Element("City").Value,
State = bEntry.Element("State").Value,
Zip = bEntry.Element("Zip").Value
}).ToList();
cityDGV.DataSource = MyList.ToList();
If this can help you
XDocument doc = XDocument.Load(Path.GetFullPath("User1.xml"));
var rows = doc.Descendants("employee").Descendants("user").Select(el => new()
{
department = el.Element("department").Value,
manager = el.Element("manager").Value,
hours = el.Element("hours").Value,
leave = el.Element("leave").Value,
});
OR from DataSet like this
DataSet ds = new DataSet();
ds.ReadXmlSchema(new StreamReader("User1.xml"));
ds.ReadXml(new StreamReader("User1.xml"));

Linq2XML separate keys into List

I want to read a config.xml and put each item into a combobox so that the XML file is the datasource. This is my code, which only gives me one entry in my combobox. How do I separate the keys? This is my filter:
C#
var xmlDocument = XDocument.Load(configfile);
var anredeItems = from key in xmlDocument.Descendants("Anrede")
select key.Value.Trim();
anredeNrComboBox.DataSource = anredeItems.ToList();
This is the XML:
<?xml version="1.0"?>
<Config>
<Anrede>
<key_1>Herrn</key_1>
<key_2>Frau</key_2>
<key_3>Herrn Dr.</key_3>
<key_4>Frau Dr.</key_4>
<key_5>Herrn Dr. Med.</key_5>
</Anrede>
</Config>
Your Descendants("Anrede") query will get you the element Andrede, and reading the Value property of that will return the concatenation of all descendant text nodes, which is what you are seeing in your combo box.
What you want are each of its child element values:
var items - doc.Descendants("Anrede")
.Elements()
.Select(x => x.Value.Trim())
.ToList();
You can change your code like this:
var xmlDocument = XDocument.Load(configfile);
var anredeItems = xmlDocument.Root.Elements("Anrede").Elements().Select(p => p.Value.Trim());
anredeNrComboBox.DataSource = anredeItems.ToList();

How to use Linq To XML to get multiple elements and store them differently?

<MainData id="1" >
<Info>
<Date>2015-06-08 15:00:00</Date>
</Info>
<Data DataRef="uu91"/>
<Data DataRef="uu92">
</Data>
</MainData>
I have an xml file and I want to take the two data element and store them into two different variable so when I do the same value comes out. when I receive these two values I would like to get the ID, Date...
var data = from item in retreiveOptaHomeFixturesXml.Descendants("MainData")
select new
{
ID = item.Attribute("id").Value,
Date = item.Element("Info").Element("Date").Value,
DataRef1 = item.Element("Data").Attribute("DataRef").Value,
Dataref2 = item.Element("Data").Attribute("DataRef").Value,
};
Ideally you should be fetching the DataRef into a list because in each MainData you will have multiple data with DataRef attribute. You can do it like this:-
var data = from item in x1.Descendants("MainData")
let dataNodes = item.Elements("Data")
select new
{
ID = item.Attribute("id").Value,
Date = item.Element("Info").Element("Date").Value,
DataRef1Ref2 = dataNodes.Select(x => (string)x.Attribute("DataRef"))
.ToList()
};

C# XML linq query

Hi I have the following XML:
<EPICORTLOG>
<POS>
<ClientId>WkStn.90.1</ClientId>
<Id>POS.90.20140819.251.8279</Id>
<StartTime>2014-08-25T05:12:34</StartTime>
<Store>90</Store>
<SysDate>2014-08-19T00:00:00</SysDate>
<TillNo>1</TillNo>
<Time>2014-08-25T05:12:34</Time>
<Tran>1093</Tran>
<WkStn>1</WkStn>
<WORKSTATION>
<IsAutoLock>1</IsAutoLock>
</WORKSTATION>
<TRADE>
<ITEM>
<Class>102499</Class>
<Desc>NIKE RACER</Desc>
<FinalPrice>82.77</FinalPrice>
<Status>ACTV</Status>
<Style>EV0615</Style>
<Tag>1</Tag>
</ITEM>
</TRADE>
</POS>
</EPICORTLOG>
There are many POS nodes like above in the actual XML. I am trying to fetch the POS node with ID=POS.90.20140819.251.8279 and then the details of Item from that particular node. I have written the following query:
XDocument xdoc = XDocument.Load(XMLFile);
var item = from items in xdoc.Element("EPICORTLOG").Descendants("POS")
where items.Attribute("Id").Value == strSelectedPOSID
select new
{
desc=items.Element("ITEM").Attribute("Desc")
};
But it is not yielding any result for me. Here strSelectedPOSID=POS.90.20140819.251.8279. Please let me know where i am going wrong.
Id and Desc are not an Attributes. they are Elements so you should use
var item = from items in xdoc.Descendants("POS")
where (string)items.Element("Id") == strSelectedPOSID
select new
{
desc = (string)items.Element("ITEM").Element("Desc")
};
I got the value at last!! Following is what i used:
var item = from items in xdoc.Element("EPICORTLOG").Descendants("POS")
where (string)items.Element("Id") == strSelectedPOSID
select new
{
desc = items.Element("TRADE").Element("ITEM").Element("Desc").Value.ToString()
};
Thanks for the inputs.

Fill datagrid or listview from XML file

I have a well formed XML file I would like to fill a datagrid with. I would prefer using the AutoGenerate feature of WFPToolKit datagrid, but could hard code the columns in.
The trouble I am having is getting the xml file contents into a datagrid. I had it partially working in a listview, but think a datagrid would be more suited for my needs.
Can anyone provide a quick example of how to accomplish this?
I bound the XML to the ListView like this:
// Bind the data to the ListView
var binding = new System.Windows.Data.Binding() {
Source = MergedXmlDataProvider,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
XPath = "//element" };
this.listView1.SetBinding(ItemsControl.ItemsSourceProperty, binding);
XML looks something like this:
<root>
<element location="here" state="now"/>
<element location="there" state="then"/>
</root>
Aha! I finally worked it out with the help of another post here. Here is what I was able to get working, adding each XML element to a list view.
XDocument xdoc = XDocument.Load("c:\\isbn.xml");
var items = from item in xdoc.Descendants("BookData")
select new
{
Title = item.Element("Title").Value,
AuthTexts = item.Element("AuthorsText").Value
};
foreach (var item in items)
{
listView1.Items.Add(new { Title = item.Title, Author = item.AuthTexts });
}

Categories

Resources