Answered Parse XML string in C sharp

  • Friday, May 18, 2012 11:05 PM
     
     

    I've read quite a few articles on XML parsing but none seem to work for my xml string. It is a return from a webservice and list a bunch of OUs:

    <xml>
     <OUListItem Name="Banks"
        distinguishedName="OU=Banks,OU=Departments,DC=domain,DC=local" /> 
     <OUListItem Name="Computers"
       distinguishedName="OU=Computers,OU=Banks,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Computers"
       distinguishedName="OU=Computers,OU=I.T.,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Computers"
       distinguishedName="OU=Computers,OU=Newspapers,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Departments"
       distinguishedName="OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Groups" distinguishedName="OU=Groups,OU=I.T.,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="I.T."
       distinguishedName="OU=I.T.,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Newspapers"
       distinguishedName="OU=Newspapers,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Service Accounts"
       distinguishedName="OU=Service Accounts,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Users"
       distinguishedName="OU=Users,OU=I.T.,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Users"
       distinguishedName="OU=Users,OU=Banks,OU=Departments,DC=domain,DC=local" />
     <OUListItem Name="Users"
       distinguishedName="OU=Users,OU=Newspapers,OU=Departments,DC=domain,DC=local" />
    </xml>

    Can anyone tell me the correct way to read the distinguished names into a combobox in C sharp? I could obviously do some string manipulation to do this but there must be a better way.

    Thank you

All Replies

  • Saturday, May 19, 2012 12:55 AM
     
     Answered Has Code

    HI .

    try following sample

    System.Xml.Linq.XDocument _xdoc = System.Xml.Linq.XDocument.Parse(Properties.Resources.String3); // Properties.Resources.String3 value is your last posted xml data string.

    var _eles = _xdoc.Descendants("OUListItem").Distinct(); foreach (var item in _eles) { this.comboBox1.Items.Add(item.Attribute("distinguishedName").Value); }

    hope to give you a hand.

    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.


    • Edited by Matthew LIN Saturday, May 19, 2012 12:56 AM
    • Marked As Answer by bondy666 Sunday, May 20, 2012 6:36 PM
    •  
  • Sunday, May 20, 2012 3:23 PM
     
     

    I have tried setting 'String3' to the XML string but I keep getting an error :
     'WebAppGUI.Properties.Resources' does not contain a definition for 'String3'

    Any ideas?

  • Sunday, May 20, 2012 6:36 PM
     
     

    Apologies, worked perfectly when I substituted Properties.Resources.String3 for the actual string (as it does indeed say to do in the comment!).

    Thanks, been bugging me all weekend.