Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:34:39+00:00 2026-06-14T09:34:39+00:00

I have an xml-like text, in which I would like to find the item

  • 0

I have an xml-like text, in which I would like to find the item that occurs in the first occurrence of a certain pattern:
typically:

...
<PropertyGroup><name>true</name></PropertyGroup><PropertyGroup>....
....

Could also be

...
<PropertyGroup>
<name>
true</name>
</PropertyGroup>
...
<PropertyGroup>
...

In the above, I need to extract the “name”.

My initial assumption was that all occurrences were to be in one line, and I wrote my code using string properties, but it is very difficult o take in consideration every possibility, and only RegEx can save me.

I just don’t know how to write it…

I Have started with something like this:

Regex regex = new Regex("(?<=<PropertyGroup>#)<+");
Match matches = regex.Matches(Text)[0];
MessageBox.Show(matches.ToString());

I think this finds the first item after a <PropertyGroup>, but I don’t know how to make it get the item within the angular brackets… (which may be after one or more newlines, and/or spaces).

I know that there are utilities for parsing xml, but I am looking for something simple to insert in a c# program

Can someone please help me ? Thank you very much.

Edit:
Actual file content (the one I am testing now, with no weird spaces):

<?xml version="1.0" ?><Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets">
    <Import xxx/>   
  </ImportGroup>
  <PropertyGroup><myProp>true</myProp></PropertyGroup><PropertyGroup Label="UserMacros"/>
  <PropertyGroup/>
<!--maybe other stuff -->  
</Project>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:34:40+00:00Added an answer on June 14, 2026 at 9:34 am

    Using LINQ to XML is really quite simple and is much more reliable than using regular expressions:

    using System.Xml.Linq;
    

    …

    XElement xmlTree = XElement.Load(fileName);
    XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
    
    List<XElement> properties = (
        from propertyGroup in xmlTree.Descendants(ns + "PropertyGroup")
        from property in propertyGroup.Elements()
        select property
    ).ToList();
    

    Now properties should contain all the XElement objects for the immediate children of all PropertyGroup elements. You can that get both their names and values with:

    foreach(var property in properties)
    {
        string name = property.Name.LocalName;
        string value = property.Value;
        // process both strings   
    }
    

    This way you do not have to worry about any kind of whitespace and it is a very maintainable and extensible solution to go on and retrieve other information from the XML.

    Plus, it is really something generally worth reading up on and here is the place to start. This will not be the last time you need to handle an XML file, and you will be glad if you do not always have to figure out a regex to parse (which, let me repeat that, is not even generally possible).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file which I'd like to parse into a non-XML (text)
I have an XML document like this: <xml> <item> <title>Article 1</title> <text><![CDATA[Lorem ipsum dolor
If I have some xml like this: <mynode> <mysubnode> <mysubsubnode>hello world</mysubsubnode> some more text
I have XML like, <items> <item> <products> <product>laptop</product> <product>charger</product> <product>Cam</product> </products> </item> <item> <products>
I have XML like this: <exam> <section name=SampleSection1> <attributes> <variable_name data_type=String value=SCORE/> </attributes> <item
I have a XML column which contains XML like this: <Set> <Element> <ID> 1
Assume that I have file XML like this : <?xml version=1.0?> <P> <Content> <id>1016576</id>
I have a sql server 2005 table which has xml store in a text
I am new to python and would like to understand parsing xml. I have
I have an XML file which contains text with some very simple layout constructs:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.