single XML file but need to populate 2 dropdownlist - c#

i have 2 dropdownlist to implement and the xml file is as follow:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder id="1" value="builder#xyz.com">
</builder>
<builder id="2" value="Others">
</builder>
</builderemail>
<manageremail>
<manager id="1" value="manager#xyz.com"></manager>
<manager id="2" value="Others"></manager>
</manageremail>
</email>
i am using visual studio 2005 and i tried to data bind each drop down list by choosing a data source. my requirements are that one of the drop downlist for builder's email and the other drop down list for manager's email.
How do i do that by using the datasource configuration wizard?
regards

These links might help you out
http://www.codeproject.com/KB/aspnet/xmlboundlistcontrol.aspx
http://dotnetspidor.blogspot.com/2009/04/bind-aspnet-dropdownlist-to-xml-file.html
But you might have to change your xml and structure it like
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<id>1</id>
<value>builder#xyz.com</value>
</builder>
</builderemail>
<manageremail>
<manager>
<id>1</id>
<value>manager#xyz.com</value>
</builder>
</manageremail>
</email>
Hope this helps

You need to parse xml and store the builderemail and manageremail in list, then provide as datasource to dropdown list
XmlDocument doc = new XmlDocument();
doc.Load(#"sample.xml");
XmlNodeList builderNodes = doc.GetElementsByTagName("builderemail");
XmlNodeList mangerNodes = doc.GetElementsByTagName("manageremail");
List<string> builderMails = new List<string>();
foreach (XmlNode node in builderNodes[0].ChildNodes)
{
builderMails.Add(node.Attributes["value"].Value);
}
List<string> mangerMails = new List<string>();
foreach (XmlNode node in mangerNodes[0].ChildNodes)
{
mangerMails.Add(node.Attributes["value"].Value);
}
comboBox1.DataSource = builderMails;
comboBox2.DataSource = mangerMails;

Related

How to show specific information on xml

I have a c# console application where I update, add and delete information from an xml which works so far. But when i show the xml in my console with WriteLine it shows like this:
<ArrayOfKunde xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Kunde id="1">
<firstName>Hasan 2</firstName>
<lastName>sad</lastName>
<adress>sdfd</adress>
<birthday>vcxbgf</birthday>
<bankDetails>bcgh</bankDetails>
</Kunde>
<Kunde id="2">
<firstName>ghf</firstName>
<lastName>nbv</lastName>
<adress>bjk</adress>
<birthday>hjvn</birthday>
<bankDetails>jhgj</bankDetails>
</Kunde>
<Kunde id="3">
<firstName>mbn,</firstName>
<lastName>hgj</lastName>
<adress>ghj</adress>
<birthday>ghjg</birthday>
<bankDetails>hghj</bankDetails>
</Kunde>
</ArrayOfKunde>
it is probably due to my code to print it:
string filepath = "customerdatabase2.xml";
var xDoc = XDocument.Load(filepath);
Console.WriteLine(xDoc);
I wanted to ask if there is any way for me to filter what is shown? For example I do not want to show:
<ArrayOfKunde xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Use: XDocument.Descendants
So your code can be:
string filepath = "customerdatabase2.xml";
var xDoc = XDocument.Load(filepath);
foreach (var node in xDoc.Descendants().Skip(1))
Console.WriteLine(node);
But this is very much tied to your Xml structure. Note that I am skipping 1st descendant because that will be the Top level ArrayOfKunde node.

insert xml tag with c#

I'm trying to insert a XML tag in an existing xml-file. To insert an XML-Tag in C# is not really a problem, but my Problem is, that i need the new beginning-tag and the ending-tag on a certain place in that file ... can anybody help me ...
here an Example:
this is the xml-file before:
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<Item>
<ItemID>2711</ItemID>
<ItemTypeID>P</ItemTypeID>
<ColorID>1</ColorID>
</Item>
<Item>
<ItemID>64566</ItemID>
<ItemTypeID>P</ItemTypeID>
<ColorID>3</ColorID>
</Item>
</DocumentElement>
... this should be the new XML-file -> with the new tag "Inventory":
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<Inventory>
<Item>
<ItemID>2711</ItemID>
<ItemTypeID>P</ItemTypeID>
<ColorID>1</ColorID>
</Item>
<Item>
<ItemID>64566</ItemID>
<ItemTypeID>P</ItemTypeID>
<ColorID>3</ColorID>
</Item>
</Inventory>
</DocumentElement>
can anybody help me?
You can wrap the existing Items in a new XElement then replace the nodes on the original document:
XDocument doc = XDocument.Parse("<DocumentElement><Item><ItemID>2711</ItemID><ItemTypeID>P</ItemTypeID><ColorID>1</ColorID></Item><Item><ItemID>64566</ItemID><ItemTypeID>P</ItemTypeID><ColorID>3</ColorID></Item></DocumentElement>");
var items = doc.Descendants("Item");
XElement inventory = new XElement("Inventory");
inventory.Add(items);
doc.Root.ReplaceNodes(inventory);
//now you can save the XDocument to a file or whatever
Creates this:
<DocumentElement>
<Inventory>
<Item>
<ItemID>2711</ItemID>
<ItemTypeID>P</ItemTypeID>
<ColorID>1</ColorID>
</Item>
<Item>
<ItemID>64566</ItemID>
<ItemTypeID>P</ItemTypeID>
<ColorID>3</ColorID>
</Item>
</Inventory>
</DocumentElement>
If you have LINQPad you can run this script: http://share.linqpad.net/7c636x.linq
You can try this
var doc = XDocument.Load(file);
var characters = doc.Descendants("DocumentElement").FirstOrDefault();
if (characters != null)
{
XElement xe = new XElement("Inventory");
characters.Add(xe);
var oColl = doc.Descendants("Item");
xe.Add(oColl);
}
doc.Save(file);

Add node and elements in xml using C#/Linq

I have an xml file like this,
<?xml version="1.0" encoding="utf-8" ?>
<root>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<ContentSelector datavalue_idtype="content:smartform" datavalue_displayvalue="«Smart Form:49»">49</ContentSelector>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>
I want to modify it like the one below,
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Title>HomePage</Title>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<Products>
<Product>
<ProductTitle>Product</ProductTitle>
<ProductDate>03-08-2012 11:57:25</ProductDate>
<ProductImage>
<img src="ex1.jpg" />
</ProductImage>
<ProductThumbnailImage>
<img src="ex2.jpg" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
<ProductImage>
<img src="ex3.jpg" />
</ProductImage>
<ProductThumbnailImage>
<img src="ex4.jpg" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
</Product>
</Products>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>
All the new nodes and values are to be added through a C# function. Let us assume these new values as static values for now.
Also the node "FeaturedProduct" is not only one. There are a lot of nodes in that name. I want to modify all the "FeaturedProduct" nodes.
You should take a look at the XDocument class here:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
I had to do something very similar to this a couple of weeks ago; XDocument is more willing to manipulate data, and using LINQ with it is incredibly easy.
This doesn't use linq exactly, but It will serve your purpose
XmlDocument xDoc = new XmlDocument();
xDoc.Load("filename.xml");
foreach (XmlNode xNode in xDoc.SelectNodes("//FeaturedProduct"))
{
XmlElement newElement = xDoc.CreateElement("newElementName");
XmlAttribute newAttribute = xDoc.CreateAttribute("AttributeName");
newAttribute.Value = "attributeValue";
newElement.Attributes.Append(newAttribute);
xNode.AppendChild(newElement);
xNode.InnerText = "myInnerText";
}
Also, This Documentation is a very handy reference for Xpath
Here's how to do it with Linq:
string documentXml = #"<?xml version=""1.0"" encoding=""utf-8"" ?>
<root>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<ContentSelector datavalue_idtype=""content:smartform"" datavalue_displayvalue=""«Smart Form:49»"">49</ContentSelector>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>";
string productsXml = #"<Products>
<Product>
<ProductTitle>Product</ProductTitle>
<ProductDate>03-08-2012 11:57:25</ProductDate>
<ProductImage>
<img src=""ex1.jpg"" />
</ProductImage>
<ProductThumbnailImage>
<img src=""ex2.jpg"" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
<ProductImage>
<img src=""ex3.jpg"" />
</ProductImage>
<ProductThumbnailImage>
<img src=""ex4.jpg"" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
</Product>
</Products>";
XDocument document = XDocument.Parse(documentXml);
var targetNodes = from featuredProduct in document.Descendants("FeaturedProduct")
from contentSelector in featuredProduct.Elements("ContentSelector")
select contentSelector;
foreach (var targetNode in targetNodes)
{
targetNode.ReplaceWith(XElement.Parse(productsXml));
}
Console.WriteLine(document.ToString());

Changing an XML Child Element into a Parent

I have an xml document that has a structure like so:
<?xml version="1.0" encoding="iso-8859-1" ?>
- <newdataset xml="version="1.0" encoding="iso-8859-1"">
- <officelist>
<officeid>2</officeid>
<office>Office</office>
<region>BC</region>
I would like to have the office id = 2 to be its own element. Like so
<?xml version="1.0" encoding="iso-8859-1" ?>
<newdataset xml="version="1.0" encoding="iso-8859-1"">
<officelist>
<officeid id=2/>
<office>Office</office>
<region>BC</region>
</officeid>
</officelist>
</newdataset>
xmlDS += offices.GetXml();
xmlDS = xmlDS.Replace(#"xml:space=""preserve""", " ");
XmlDocument doc = new XmlDocument();
XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
doc.LoadXml(xmlDS);
doc.Save(Response.OutputStream);
That is my code so far... not sure how to set a child node to become a parent node
using XDocument made this a lot easier with Linq.

linq to xml. read. and assign to ViewData..noob

I have some xml similar to this:
<?xml version="1.0" encoding="utf-8" ?>
<data>
<resources>
<resource key="Title">Alpha</resource>
<resource key="ImageName">Small.png</resource>
<resource key="Desc">blah</resource>
</resources>
</data>
using linq-xml how can i assign each resource here as a key value pair with the ViewData collection.
Thanks.
var doc = XDocument.Parse(documentString);
foreach (var res in doc.Root.Descendants("resources")) {
ViewData[(string) res.Attribute("key")] = res.Value;
}
Should work.
assuming you loadt hat xml into an XDocument, you can just iterate on teh descendants. here's a quick example, if it's coming from a string:
var doc = XDocument.Parse(docAsString);
foreach (var resource in doc.Descendants("resource"))
ViewData[resource.Attribute("key").Value] = resource.Value;

Categories