Ask a questionAsk a question
 

AnswerMatch Values inside Tags as

Answers

  • Saturday, October 31, 2009 1:58 AMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    The pattern I gave earlier achieved that. Here's a complete example to demonstrate both the regex and LINQ to XML approaches:

    // sample of valid xml
    string xml = @"<Books>
    <BookID id=""1234569@XYZ2"" Duplicateid=""1234569@XYZ2"">
    <name>XYZ</name>
    </BookID>
    <BookID id=""123@ABC1"" Duplicateid=""456@ABC2"">
    <name>ABC</name>
    </BookID>
    <BookID id=""789@HIJ22"" Duplicateid=""321@HIJ90"">
    <name>HIJ</name>
    </BookID>
    </Books>";
    
    // Using Regex
    string pattern = @"\bid=""(?<Id>[^""]+)""\s+Duplicateid=""(?<DuplicateId>[^""]+)""";
    foreach (Match m in Regex.Matches(xml, pattern))
    {
        Console.WriteLine("Id: {0} -- DuplicateId: {1}", m.Groups["Id"].Value, m.Groups["DuplicateId"].Value);
    }
    
    // Using LINQ to XML
    XDocument doc = XDocument.Parse(xml);
    var query = from element in doc.Root.Elements("BookID")
                select new
                {
                    Id = element.Attribute("id").Value,
                    DuplicateId = element.Attribute("Duplicateid").Value
                };
                
    foreach (var book in query)
    {
        Console.WriteLine("Id: {0} -- DuplicateId: {1}", book.Id, book.DuplicateId);
    }
    


    You really need to play with LINQ to get familiar with it. If you're interested in LINQ to XML you can get started here . I also highly recommend Joseph Albahari's free LINQPad tool . It is a light-weight snippet compiler with support for LINQ to SQL (and all LINQ flavors). In addition, it comes with sample code from his book, C# 3.0 in a Nutshell, which is helpful in demonstrating how LINQ works.
    Document my code? Why do you think it's called "code"?
    • Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 8:05 AM
    • Edited byAhmad Mageed Monday, November 23, 2009 4:46 PMremoved LINQPad Dump() extension from code
    •  

All Replies

  • Tuesday, October 27, 2009 8:47 PMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Is there any reason you can't use LINQ to XML to get the attributes? That would be ideal.

    Otherwise, how about this:

    string input = @"<BookID id=""1234569@XYZ2"" Duplicateid=""1234569@XYZ2"">
                        <name>XYZ bla bla</name>
                    </BookID>";
    string pattern = "=\"(?<Value>[^\"]+)\"";
    foreach(Match m in Regex.Matches(input, pattern))
    {
        Console.WriteLine(m.Groups["Value"].Value);
    }
    

    You could make the pattern stricter, if needed, but this should get you started.


    Document my code? Why do you think it's called "code"?
  • Wednesday, October 28, 2009 12:37 AMccbristo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    If possible, I would try to use either Linq to XML or the XmlDocument class to get at this information.  It will be much easier to accurately identify the attributes and their values using these classes than it will be using regular expressions.
  • Friday, October 30, 2009 1:04 PMMAbshir Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thank you both for the response.

    I haven't played with LINQ and not sure how it works.

    Without too much details, i wonder how it works?

    Someone would load the source file to LINQ, then there is LINQ to XML functions/properties??

    Also, in the above pattern; i meant the file will contain many lines each of wich is as follows, but values in it will be different each line: I just put the seperator to show you there will be morethan one line.
    I can't put the numbers 1234569@XYZ because each line will different. can i use pattern that gets values behind the
    BookID id=  and stops at the closing quotes like.


    <BookID id="1234569@XYZ2" Duplicateid="1234569@XYZ2">

    <name>XYZ bla bla</name>

    </BookID>
    -------------------------------------------------------------------------------------------------------
    <BookID id="1234569@XYZ2" Duplicateid="1234569@XYZ2">

    <name>XYZ bla bla</name>

    </BookID>
    --------------------------------------------------------------------------------------------------------
    <BookID id="1234569@XYZ2" Duplicateid="1234569@XYZ2">

    <name>XYZ bla bla</name>

    </BookID>

  • Saturday, October 31, 2009 1:58 AMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    The pattern I gave earlier achieved that. Here's a complete example to demonstrate both the regex and LINQ to XML approaches:

    // sample of valid xml
    string xml = @"<Books>
    <BookID id=""1234569@XYZ2"" Duplicateid=""1234569@XYZ2"">
    <name>XYZ</name>
    </BookID>
    <BookID id=""123@ABC1"" Duplicateid=""456@ABC2"">
    <name>ABC</name>
    </BookID>
    <BookID id=""789@HIJ22"" Duplicateid=""321@HIJ90"">
    <name>HIJ</name>
    </BookID>
    </Books>";
    
    // Using Regex
    string pattern = @"\bid=""(?<Id>[^""]+)""\s+Duplicateid=""(?<DuplicateId>[^""]+)""";
    foreach (Match m in Regex.Matches(xml, pattern))
    {
        Console.WriteLine("Id: {0} -- DuplicateId: {1}", m.Groups["Id"].Value, m.Groups["DuplicateId"].Value);
    }
    
    // Using LINQ to XML
    XDocument doc = XDocument.Parse(xml);
    var query = from element in doc.Root.Elements("BookID")
                select new
                {
                    Id = element.Attribute("id").Value,
                    DuplicateId = element.Attribute("Duplicateid").Value
                };
                
    foreach (var book in query)
    {
        Console.WriteLine("Id: {0} -- DuplicateId: {1}", book.Id, book.DuplicateId);
    }
    


    You really need to play with LINQ to get familiar with it. If you're interested in LINQ to XML you can get started here . I also highly recommend Joseph Albahari's free LINQPad tool . It is a light-weight snippet compiler with support for LINQ to SQL (and all LINQ flavors). In addition, it comes with sample code from his book, C# 3.0 in a Nutshell, which is helpful in demonstrating how LINQ works.
    Document my code? Why do you think it's called "code"?
    • Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 8:05 AM
    • Edited byAhmad Mageed Monday, November 23, 2009 4:46 PMremoved LINQPad Dump() extension from code
    •  
  • Wednesday, November 04, 2009 4:20 PMVQ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    The key to these sort of expression is the Non-Greedy Quantifier.

    @"id=""(.*?)"""
    



  • Friday, November 06, 2009 6:29 AMeryangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi MAbshir,
    Are those replies helpful for you, or you still have any concern about this issue? It will be very beneficial for other community members having the similar questions if  you mark useful replies as answers.

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
  • Monday, November 23, 2009 4:04 PMMAbshir Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Eric,
    Yeah the response was helpfull. I got off track and just getting back to the the linkQ thing.
     Sorry, i thought i marked it.

    Appreciated everybody's help.