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

  • SEARCH
  • Home
  • 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 7248149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:05:59+00:00 2026-05-28T22:05:59+00:00

Here are the problems with the LINQ to XML below: Is there a way

  • 0

Here are the problems with the LINQ to XML below:

  • Is there a way to return an enumeration of the attributes so I don’t have to do the foreach loop?

  • It should only return elements that have a cardnum between 13 and 16 digits, but it appears to be returning numbers longer than that? Why?

  • Is long.TryParse the best way to test if the 16 digit number is in fact a number?

  • Also, is it possible to return not only elements that have attributes with 16 digit numbers, but also elements with inner text such as <ccnum>1234567890123456</ccnum> and then parse every child node of the parent node of <ccnum>, so for example, the xml would look like this:

     <details>
     <ccnum>283838383838383838</ccnum>
     <cvv>399</cvv>
     <exp>0202</exp>
     <name>joe</name>
     </details>
    

Here is the code:

    long numeric;

    string xml = @"<Details>
    <CreditCard cardnum='1234888888823456'
    ccv='123' 
    exp='0212' 
    cardType='1' 
    name='joe' />
    <CreditCard cardnum='123488888882345633333'
    ccv='123' 
    exp='0212' 
    cardType='1' 
    name='joe' />
    </Details>";

    XElement element = XElement.Parse(xml);
    IEnumerable<XElement> elementsWithPossibleCCNumbers = 
        element.Descendants()
               .Where(d => d.Attributes()
                            .Where(a => a.Value.Length >= 13 && a.Value.Length <= 16)
                            .Where(a => long.TryParse(a.Value, out numeric))
                            .Count() == 1).Select(x=>x);


    foreach(var x in elementsWithPossibleCCNumbers)
    {
        foreach(var a in x.Attributes())
        {
        //Check if the value is a number
        if(long.TryParse(a.Value,out numeric))
        {
            //Check if value is the credit card
            if(a.Value.Length >= 13 && a.Value.Length <= 16)
                xml = xml.Replace(a.Value, string.Concat(new String('*',a.Value.Length - 4),a.Value.Substring(a.Value.Length - 4)));
            else //If value is not a credit card, replace it with ***
                xml = xml.Replace(a.Value, "***");
        }
      }
    }

OK, I got why it I thought it was returning the number longer than 16, it was because, the first 16 digits are the same as the first number and I am just replacing that part, so I guess that brings up the question of how to just update the correct attribute.

Is a solution to updating the whole number is to use a regex boundary?

  • 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-05-28T22:05:59+00:00Added an answer on May 28, 2026 at 10:05 pm
    • To avoid the foreach loop:

      var element = XElement.Parse(xml);
      var elementsWithPossibleCCNumbers =
          element.Descendants()
                  .Where(d => d.Attributes()
                      .Where(a => a.Value.Length >= 13 && a.Value.Length <= 16)
                      .Count(a => long.TryParse(a.Value, out numeric)) == 1);
      
      elementsWithPossibleCCNumbers
          .SelectMany(e => e.Attributes())
          .Where(a => long.TryParse(a.Value, out numeric))
          .ToList()
          .ForEach(a => a.Value = a.Value.Replace(a.Value, MaskNumber(a.Value)));
      

    and declare this method:

        static string MaskNumber(string numericValue)
        {
            if (numericValue.Length >= 13 && numericValue.Length <= 16)
                return new String('*', numericValue.Length - 4) + numericValue.Substring(numericValue.Length - 4);
    
            return "***";
        }
    
    • It should only return elements that have a cardnum between 13 and 16 digits […] – glad you sorted that out 🙂

    • I think long.TryParse is a good way to check if all characters are digits. Alternatively, you could use the regex that @Henk Holterman suggested in his answer – that also gets rid of the Length comparison, making the code shorter and more readable.

    • In the case of elements with inner text, you should use element.Value instead of foreach(a in element.Attributes) -> a.Value

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

Sidebar

Related Questions

Looking for some direction here as I'm running into some migration problems. We have
I'm new around here and i have a little problems with a C# application.
I have problems with my keyup binding when cloning an element. Here's the scenario:
I am trying to read an XML file using LINQ. I have had no
Here's what I'm trying to do. I'm querying an XML file using LINQ to
OK, I asked for how to return a Linq query results as XML, and
While trying to use LINQ to SQL I encountered several problems. I have table
Edit: I decided to take the LINQ to XML approach (see the answer below)
I'm using LINQ to XML. This is the line which I'm having problems: var
I've been playing around with some linq to xml, and I'm having some problems

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.