Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nigel Wilson 945 posts 2077 karma points
    Jul 15, 2010 @ 04:12
    Nigel Wilson
    0

    Convert specific nodes to a drop down list within a C# user control

    Hey

    I am wanting to filter an XML based on the value within a data field - the XML data I am traversing.

    My c# code is

    XmlNodeList xnList = xmlDoc.SelectNodes("node [/data[@alias='MyExampleCustomField'] = '1']");
    
    foreach (XmlNode xn in xnList)
    {
       string nodeID = xn.InnerText;
       DropDownList.Items.Add(new ListItem(nodeID, nodeID));
    }

     

    Whilst the page loads I am getting no output. So is anyone able to enlighten on :

     

    1. How to get the filtered nodeset with the SelectNodes method
    2. How to get the id and name from the filtered nodes

     

    Thanks

    Nigel

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 15, 2010 @ 07:49
    Aaron Powell
    0

    Why don't you use the nodeFactory API if you're working in .NET?

    var node = new Node(1234); //pass in the ID of the node that's your container node
    foreach(Node n in node.Children.Where(x => x.GetProperty("MyExampleCustomField") == "1")) {
        DropDownList.Items.Add(new ListItem(n.Id.ToString(), n.Id.ToString()));
    }

    Basically you load up your parent node, then use LINQ to filter the children for ones which match what you want.

    That may not be 100% syntactically correct, but it's close and should get you started.

  • Nigel Wilson 945 posts 2077 karma points
    Jul 15, 2010 @ 10:18
    Nigel Wilson
    0

    Hi Slace

    Thanks for your feedback.

    My bad on the post - I could (or maybe should) have included a bit more information, however didn't want to clutter the post with too much info ! Damned if you do, damned if you don't !!

    Basically I have created an xml file of members and am trying to do get the data from that file. The structure of my XML file matches that of the standard Umbraco XML file - <node><data (with alias)>, etc

    So I first load the XML doc and then I was wanting to apply a filter to get some specific node values. An excerpt of my XML file is below:

    <?xml version="1.0" encoding="utf-8"?> 
    <root id="-1">
      <node id="7787" parentID="-1" path="-1" level="1">
        <data alias="LegalName">Asmithy Smith And Smith</data>
        <data alias="TradingName">Blah Blah And Smith</data>
        <data alias="PhysicalCity">Rangiora</data>
        <data alias="foodstuffsGroup" />
        <data alias="StoreID" />
        <data alias="ContactEmail">[email protected]</data>
        <data alias="PrimaryBusinessCategory" />
      </node>

    Cheers

    Nigel

  • Nigel Wilson 945 posts 2077 karma points
    Jul 16, 2010 @ 04:04
    Nigel Wilson
    0

    Yee hah - have managed to figure it out . . .

    //Populate XDropDownList from XML file
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("d:/MyXMLFile.xml");
    XmlNodeList xnList = xmlDoc.SelectNodes("root/node[data[@alias='foodstuffsGroup']='1']");
    
    foreach (XmlNode xn in xnList)
    {
            DropDownList.Items.Add(new ListItem(xn.SelectSingleNode("data[@alias='LegalName']").InnerText,xn.Attributes[0].Value));
    }

    Not sure the above will be totally useful for Umbraco-uvians but thought I'd post it as a conclusion to my wee challenge! 

     

Please Sign in or register to post replies

Write your reply to:

Draft