i have Some Xml stored which is been generated from another Source
the xml contains certain unwanted text which i need to remove them
<IceCream="Vanilla"/>
<assignedPerson>
-1- <<<<<<<<< Need to Remove this text while Reading Xml
<name>
<prefix>Mr..</prefix>
<name1>Martin</name1>
</name>
</assignedPerson>
public string RemoveUnWantedCharsfromXml(string XmlFile)
{
string Xml = string.Empty;
Regex regex = new Regex(@"-[0-9]+-", RegexOptions.IgnoreCase);
if (regex.IsMatch(XmlFile))
Xml = regex.Replace(XmlFile, String.Empty);
return Xml;
}
san i couldn;t use method bcuz it kept giving me error in String.Join
The best overload method is string[]
the xml contains certain unwanted text which i need to remove them
Hello:)
First I'd to say that I've tested your codes,it works great:
namespace A
{
class Program
{
public static string RemoveUnWantedCharsfromXml(string XmlFile)
{
string Xml = string.Empty;
Regex regex = new Regex(@"-[0-9]+-", RegexOptions.IgnoreCase);
if (regex.IsMatch(XmlFile))
Xml = regex.Replace(XmlFile, String.Empty);
return Xml;
}
static void Main(string[] args)
{
string s = "<IceCream=\"Vanilla\"/><assignedPerson>-1-<name><prefix>Mr..</prefix><name1>Martin</name1></name></assignedPerson>";
s = RemoveUnWantedCharsfromXml(s);
Console.WriteLine(s);
}
}
}
If you are sure that you only want to remove "-1"——Just use this following:
string s = "Your xml contents";
s=s.Replace("-1-","");
tan_vision_1...
Participant
1178 Points
413 Posts
Remove Certain Text from Xml
Jan 07, 2012 04:49 AM|LINK
i have Some Xml stored which is been generated from another Source
the xml contains certain unwanted text which i need to remove them
celinajohn
Member
182 Points
52 Posts
Re: Remove Certain Text from Xml
Jan 07, 2012 05:37 AM|LINK
You can remove it by using REGEX class of c#
tan_vision_1...
Participant
1178 Points
413 Posts
Re: Remove Certain Text from Xml
Jan 07, 2012 06:14 AM|LINK
i tried this but it's not working
//Regex rgx = new Regex(@"-[0-9]-"); //string Xml = rgx.Replace(value, string.Empty);san SanZ
Member
296 Points
60 Posts
Re: Remove Certain Text from Xml
Jan 07, 2012 06:42 AM|LINK
HI IF YOUR XML FILE IS ALWAYS SAME
try this,
var str = xm1.Split('\n').Where(a => a.StartsWith("<")).Select(a=>a.Replace('\r',' ').Trim()); var res=String.Join("", str);-- If you find this post helpful then please "Mark As Answer"
tan_vision_1...
Participant
1178 Points
413 Posts
Re: Remove Certain Text from Xml
Jan 07, 2012 07:10 AM|LINK
Made New Method and it Worked
public string RemoveUnWantedCharsfromXml(string XmlFile) { string Xml = string.Empty; Regex regex = new Regex(@"-[0-9]+-", RegexOptions.IgnoreCase); if (regex.IsMatch(XmlFile)) Xml = regex.Replace(XmlFile, String.Empty); return Xml; } san i couldn;t use method bcuz it kept giving me error in String.Join The best overload method is string[]Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Remove Certain Text from Xml
Jan 09, 2012 12:17 AM|LINK
Hello:)
First I'd to say that I've tested your codes,it works great:
namespace A { class Program { public static string RemoveUnWantedCharsfromXml(string XmlFile) { string Xml = string.Empty; Regex regex = new Regex(@"-[0-9]+-", RegexOptions.IgnoreCase); if (regex.IsMatch(XmlFile)) Xml = regex.Replace(XmlFile, String.Empty); return Xml; } static void Main(string[] args) { string s = "<IceCream=\"Vanilla\"/><assignedPerson>-1-<name><prefix>Mr..</prefix><name1>Martin</name1></name></assignedPerson>"; s = RemoveUnWantedCharsfromXml(s); Console.WriteLine(s); } } }If you are sure that you only want to remove "-1"——Just use this following:
string s = "Your xml contents"; s=s.Replace("-1-","");Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Remove Certain Text from Xml
Jan 13, 2012 01:07 AM|LINK
Hello:)
var res=String.Join("", str.ToArray());