Question

My program is currently grabbing all attribute values when I only want a specific one depending on which list box item is checked. Any help would be greatly appreciated, thanks in advance!

XML:

<Products>
<Equity>
<servers>
    <serverEQ>server1</serverEQ>
    <serverEQ>server2</serverEQ>
    <serverEQ>server3</serverEQ>
</servers>
<sitesE>
    <sitesEQ sitePathEQ="\Logs\W3SVC1"><nameEQ>SystemAdmin Site</nameEQ></sitesEQ>
    <sitesEQ sitePathEQ="\Logs\W3SVC3"><nameEQ>Direct Access Site</nameEQ></sitesEQ>
    <sitesEQ sitePathEQ="\Logs\W3SVC4"><nameEQ>Redirect Site</nameEQ></sitesEQ>
    <sitesEQ sitePathEQ="\Logs\W3SVC5"><nameEQ>Download Site</nameEQ></sitesEQ>
</sitesE>
</Equity>
.
.
.
</Products>

C#:

myXML.siteName = "sitesEQ";
myXML.sitePath = "sitePathEQ";
.
.
.  
private void Submit_btn_Click(object sender, EventArgs e)
    {
        XmlElement root = MYproducts.DocumentElement;
        XmlNodeList sitelist = root.GetElementsByTagName(myXML.siteName);

   foreach (object ServerChecked in serverLISTbox.CheckedItems)
        {
            string MyServerChecked = ServerChecked.ToString();

            MessageBox.Show(MyServerChecked);
            foreach (object SiteChecked in siteLISTbox.CheckedItems)
            {

                foreach (XmlNode s in sitelist)
                {

                    myXML.xmlAttributes = s.Attributes[myXML.sitePath].Value;
                    MessageBox.Show(myXML.xmlAttributes);
                }
    }

myXML.xmlAttributes is displaying all attributes regardless of what I have checked.

For example: If I have "Redirect Site" checked I only want the attribute "\Logs\W3SVC4" not all of them.

Was it helpful?

Solution

Your code is quite complex for such a simple problem so, let's break it in pieces.

First of all, you need a method which will return the attribute values for the elements you pass as argument

private IEnumerable<string> GetAttributeValuesForElements(IEnumerable<string> elementNames)
{
    var document = XDocument.Load("path-to-your-xml");
    var results = document.Descendants(myXml.siteName)
        .Join(elementNames,
            element => element.Elements().First().Value,
            name => name,
            (element, name) => element.Attribute(myXml.sitePath).Value);
    return results;
}

Second of all, you need a method which will return the selected names from the listbox:

private IEnumerable<string> GetSelectedNames(Listbox listbox)
{
    foreach(var item in listbox.CheckedItems)
        yield return item.ToString();
}

Now, put those two together in your event handler:

private void Submit_btn_Click(object sender, EventArgs e)
{
    var selected = GetSelectedNames(serverLISTbox);
    var attributes = GetAttributeValuesForElements(selected);
    // do stuff with the attributes.
}

Hope this helps...

OTHER TIPS

You are looping thru the sitelist so you will always get all site paths.

You will need to add a check inside the siteLISTbox.CheckedItems for loop to check and return the site path for the SiteChecked value.

Hope this helps...

use Linq to XML

var xmlstring = @"<Products>
                <Equity>
                <servers>
                    <serverEQ>server1</serverEQ>
                    <serverEQ>server2</serverEQ>
                    <serverEQ>server3</serverEQ>
                </servers>
                <sitesE>
                    <sitesEQ sitePathEQ=""\Logs\W3SVC1""><nameEQ>SystemAdmin Site</nameEQ></sitesEQ>
                    <sitesEQ sitePathEQ=""\Logs\W3SVC3""><nameEQ>Direct Access Site</nameEQ></sitesEQ>
                    <sitesEQ sitePathEQ=""\Logs\W3SVC4""><nameEQ>Redirect Site</nameEQ></sitesEQ>
                    <sitesEQ sitePathEQ=""\Logs\W3SVC5""><nameEQ>Download Site</nameEQ></sitesEQ>
                </sitesE>
                </Equity>
                </Products>";

var xml = XDocument.Parse(xmlstring);
var node = xml.Descendants("sitesEQ").Where(d => d.Descendants("nameEQ")
                                     .Any(n => n.Value == "Redirect Site"));
var attribute = node.Attributes().FirstOrDefault();
var logValue = attribute.Value;

You can use XDocument.Load("path to xml file") if you need to load a file. I only did it as a parsed string to create a working example.

Its the var node = line that does most of the work. First, you grab all descendants of root named "sitesEQ", then you search those for any that have a "nameEQ" of "Redirect Site". Obviously, you'd want to enter your variable names in place of the strings. Then it simply grabs the first attribute and pulls off its value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top