tags:

views:

162

answers:

5

Hi, I've an xml file like this

<div class="details">
  <a href="/Details/Empinfo.asp?empid=134">Employee details</a> 
</div>

now i want to get the empid (i.e 134) from the given xml file in c# winforms. Can somebody help me how to get the emp id

Thanks in advance

+1  A: 
  1. First load the xml file in a XMLDocument/XMLReader, then get to the href attribute value which is "/Details/Empinfo.asp?empid=134".
  2. Then use this to instantiate a new Uri class and then use the Uri.Query property which will get you "?empid=134".
  3. Now look for the first = and extract the entire subsstring after that which should get you 134
logicnp
Invalid URI: The format of the URI could not be determined. It is showing like this because the url retieves like this "/Details/Empinfo.asp?empid=134 so it is not recogniging as a valid uri.
Nagu
You cna just skip using the Uri class and proceed to step 3.
logicnp
A: 

If you're sure the xml file won't contain other digits you could simply do:

Regex.Match(File.ReadAllText(filename), @"\d+").Value

Also if you have multiple IDs, you could use Regex.Matches. Otherwise you have to parse the xml file to find the right element as pointed out in another answer.

Update per comments:

Regex.Match(File.ReadAllText(filename), 
    @"empid=(?<EmployeeID>[^""]+)").Groups["EmployeeID"].Value
Yuriy Faktorovich
No my xml may contain alphanumerics also like emp101
Nagu
@Nagu, can you put that detail into your answer? Where would it contain it?
Yuriy Faktorovich
<div class="details"> <a href="/Details/Empinfo.asp?empid=emp134">Employee details</a> </div> like this
Nagu
A: 

Try something like this:

using System;
using System.Linq;
using System.Xml.Linq;
using System.Web;

class Example
{
    static void Main()
    {
     String xml = @"<div class=""details"">
        <a href=""/Details/Empinfo.asp?empid=134"">
       Employee details</a> 
      </div>";

     String[] queryString = XElement.Parse(xml)
      .Descendants("a")
      .Attributes("href")
      .FirstOrDefault()
      .Value
      .Split('?');

     String empId = HttpUtility
      .ParseQueryString(queryString[1])["empid"];
    }
}

I wouldn't recommend using regular expressions for something like this - it is better to use an XML API of some sort (I am using LINQ to XML here) and then parse the resulting string.

Andrew Hare
+1  A: 
function NameYourFunction(string xmlFile){
   XmlDocument xml = new XmlDocument();
        xml.Load(xmlFile);
        XmlNode xnode = xml.DocumentElement;
        for (int i = 0; i < xnode.ChildNodes.Count; i++)
        {
            string href = xnode.ChildNodes[i].Attributes["href"].Value;
            string empid = href.Substring(href.IndexOf('='),(href.Length -    href.IndexOf('='))+1);
        }

}

Please check it out. Haven't tried it yet.

junmats
+3  A: 

For quick EmpID, see following:

    string xml = @"<div class=""details""><a href=""/Details/Empinfo.asp?empid=134"">Employee details</a></div>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    string strEmpID = node.Attributes[@"href"].Value.ToString().Split(new char[] { '=' })[1];

Should you want all of the EmpIDs, then can do something like following:

private List<string> GetAllEmpIDs(string xml, string strTag/*Example: @"href" */, char[] caSplitBy, int nItemNumber /*second item in array, 1*/)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);

    List<string> lstEmpID = new List<string>();
    foreach(XmlNode node in doc.DocumentElement.ChildNodes)
    {
        lstEmpID.Add(node.Attributes[strTag].Value.ToString().Split(caSplitBy)[nItemNumber]);
    }

    return lstEmpID;
}

Usage:

    List<string> lstEmpID= GetAllEmpIDs(@"<div class=""details""><a href=""/Details/Empinfo.asp?empid=134"">Employee details</a></div>",
                @"href",
                new char[] { '=' },
                1
                );

Let me know, if above is of no help! (0:

KMan
Yes I'm verymuch near to this. I'll try thank you
Nagu
I've tested it already, btw. (0:
KMan
Thank you it is working fine
Nagu
Glad, I was able to help.
KMan