0

I have an xml that has several attributes and values such as follows:

<z:row ID="1"
       Author="2;#Bruce, Banner"
       Editor="1;#Bruce, Banner"
       FileRef="1;#Reports/Pipeline Tracker Report.xltm" 
       FileDirRef="1;#Reports" 
       Last_x0020_Modified="1;#2014-04-04 12:05:56" 
       Created_x0020_Date="1;#2014-04-04 11:36:21" 
       File_x0020_Size="1;#311815" 
/>

How can I remove the string from after the " up to the #?

Original

'Author="2;#Bruce, Banner"'

Converted

'Author="Bruce, Banner"'

1
  • 1
    Why don't you parse the author attribute out of the xml and then do a simple replace on the string: author = author.Replace(";#", string.Empty); Commented Apr 16, 2014 at 9:28

6 Answers 6

3

See if this helps.

private string FilterValue(string input)
{
    // If the string does not contain #, return value
    if (!input.Contains("#"))
        return input;

    // # does exist in the string so 
    // 1) find its location
    // 2) Read everything from that point to the end of the string
    // 3) Return the SubString value
    var index = input.IndexOf("#", StringComparison.Ordinal) + 1;
    return input.Substring(index, input.Length - index);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, Thanks for the post, but what this does is removes everything before the first #. Making the xml look like: Bruce, Banner" Editor="1;#Bruce, Banner" FileRef="1;#Reports/Pipeline Tracker Report.xltm" FileDirRef="1;#Reports" Last_x0020_Modified="1;#2014-04-04 12:05:56" Created_x0020_Date="1;#2014-04-04 11:36:21" File_x0020_Size="1;#311815" />
@SharePointDummy you may forget the +1 at the end. working example
From what I understand now is that you are treating the XML posted as a whole string and you want to remove all values that appears between " and #? If that's what you are doing then I would strongly suggest not to do this and use XmlDocument class instead to read/amend the values.
Hey thanks for the tip. Could you point me to an example please? much appreciated
3

Something like this ?

// same logic then M Patel.
// This one will fit only if you have three items to remove (one digit, one semi-colon and one sharp).
// use M Patel solution
string CleanElement(string elem)
{
    return elem.Substring(3, elem.Length - 3);
}

or like this :

// slower I guess but still a solution
string CleanElement(string elem)
{
     string[] strs = elem.Split('#');
     strs[0] = "";
     return string.Join("", strs);
}

Comments

2

You can use string.Substring and string.IndexOf methods

string value= node.Attributes["Author"].Value;
value=value.Substring(0, value.IndexOf('#'));

I hope this is what you are looking for assuming that you are already reading your node from xml document

If you are new to reading XML in c#, I would recommend you to take a look at following msdn link https://msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx

1 Comment

Substring is faster than Remove for this case. source
-1

You can use regex for for seraching you pattern and use regEx.Replace() method. Regex might goes like this "\d;#".

3 Comments

Regex is to powerful for this. You really dont need it.
@ aloisdg - Can you please explain how it over power this stuff ? i think performing looping or string operations will be more costlier.
regex is a mess to debug and regex often results in slower code than imperative loop. There are some tricks to improve it (compile it, use static etc.) If you want more info about it : here you go !
-1

It should work if entry is 2;#Bruce, Banner!

 string value= node.Attributes["Author"].Value;
 var op = value.Split('#');
 string name = op[1];

If other # is expected then,

string value1 = value.Substring(3, value.Length - 3);

1 Comment

What if there is a entry like 'Author="2;#Bruce, #Banner"' ?
-2

You can use a simple regex:

string s = @"<z:row ID=""1""
   Author=""2;#Bruce, Banner""
   Editor=""1;#Bruce, Banner""
   FileRef=""1;#Reports/Pipeline Tracker Report.xltm"" 
   FileDirRef=""1;#Reports"" 
   Last_x0020_Modified=""1;#2014-04-04 12:05:56"" 
   Created_x0020_Date=""1;#2014-04-04 11:36:21"" 
   File_x0020_Size=""1;#311815""
    />";

string result = Regex.Replace(s,"\"([0-9];#)","");

3 Comments

Hey thanks, that kind of worked for the attributes that have only 1 digit numeral in front of it like Author=""2;#Bruce, Banner"" What can I do for those that have 2,3 digits?
Change the pattern to "\"([0-9]+;#)"
Regex is not a good solution for this. Use a XML container like XMLDocument to get nodes and use @M-Patel method (or mine) to clean it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.