[英]c# Remove string from XML
I have an xml that has several attributes and values such as follows: 我有一个具有几个属性和值的xml,如下所示:
<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"'
'Author =“ 2; #Bruce,Banner”“
Converted 转换
'Author="Bruce, Banner"'
'Author =“ Bruce,横幅广告”
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);
}
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);
}
You can use string.Substring
and string.IndexOf
methods 您可以使用
string.Substring
和string.IndexOf
方法
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 我希望这是您正在寻找的假设您已经从xml文档中读取了您的节点
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 如果您不熟悉C#中的XML,我建议您看一下以下msdn链接https://msdn.microsoft.com/zh-cn/library/cc189056(v=vs.95).aspx
You can use regex for for seraching you pattern and use regEx.Replace() method. 您可以使用regex进行模式搜索,并使用regEx.Replace()方法。 Regex might goes like this "\\d;#".
正则表达式可能类似于“ \\ d;#”。
It should work if entry is 2;#Bruce, Banner! 如果输入为2,它将正常工作; #Bruce,横幅!
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);
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];#)","");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.