I am new to C# programming and XML. I wanted to change the string (which starts with @ and end with ') with the date (which I will pick from date picker) Please have a look on below XML file
我对c#编程和XML很陌生。我想更改字符串(以@开头,以'结尾)和日期(我将从日期选择器中选择),请查看下面的XML文件
<steps>
<step1>drop table emp1 purge</step1>
<step2>create table emp1 as select e1.first_name ,e1.last_name ,e1.phone_number ,e1.salary ,e1.hire_date from employees e1 where e1.hire_date between '@m_start_date' to '@m_end_date' group by e1.first_name ,e1.last_name ,e1.phone_number ,e1.salary ,e1.hire_date</step2>
<step3>select * from emp1</step3>
</steps>
In the above Xml code I wanted to change @m_start_date with '01-sep-2012' and @m_end_date with '30-sep-2012' The most important point is the strings @m_start_date and @m_end_date are not fixed it my change in other xml file (i.e. it may be @wk_start_dte and wk_end_dte) so I need a logic in C# where we can find a string starts with @ and end with ' so we can replace this string with date.
在上面的Xml代码我想改变@m_start_date与“01 - 9 - 2012”和@m_end_date 30 - 9月- 2012年的最重要的一点是字符串@m_start_date @m_end_date并不是固定我的改变在其他Xml文件(即可能@wk_start_dte和wk_end_dte)所以我需要一个逻辑在c#中,我们可以找到一个字符串从@开始和结束的我们可以用日期替换此字符串。
The expected result should be like this
预期的结果应该是这样的
<steps>
<step1>drop table emp1 purge</step1>
<step2>create table emp1 as select e1.first_name ,e1.last_name ,e1.phone_number ,e1.salary ,e1.hire_date from employees e1 where e1.hire_date between '01-sep-2012' to '30-sep-2012' group by e1.first_name ,e1.last_name ,e1.phone_number ,e1.salary ,e1.hire_date</step2>
<step3>select * from emp1</step3>
</steps>
Can you please help me to get this logic in C#?
你能帮我把这个逻辑输入c#吗?
2 个解决方案
#1
0
Suppose your XML is in a string called test:
假设您的XML在一个名为test的字符串中:
test = System.Text.RegularExpressions
.Regex.Replace(test, "'[^']*start[^']*'", "'01-sep-2012'");
test = System.Text.RegularExpressions
.Regex.Replace(test, "'[^']*end[^']*'", "'30-sep-2012'");
#2
0
On top of Ivan's answer about text replacement, probably you will need to do following to find the node
在Ivan关于文本替换的回答之外,您可能需要执行以下操作来查找节点
XmlDocument xDoc = new XmlDocument();
xDoc.Load("yourXml.XML");
XmlNode nodetoChange = xDoc.DocumentElement.SelectSingleNode("step2");
string xmlValue = nodetoChange.InnerText;
//Now apply Ivan's logic to replace text as you wish so that xmlValue new text
//then continue to save xml as shown below.
nodetoChange.InnterText = xmlValue;
xDoc.Save(); //In case you need to save it on hard-disk.
xDoc = null;
Hope that helps, Thanks Milind
希望这能有所帮助,谢谢Milind
#1
0
Suppose your XML is in a string called test:
假设您的XML在一个名为test的字符串中:
test = System.Text.RegularExpressions
.Regex.Replace(test, "'[^']*start[^']*'", "'01-sep-2012'");
test = System.Text.RegularExpressions
.Regex.Replace(test, "'[^']*end[^']*'", "'30-sep-2012'");
#2
0
On top of Ivan's answer about text replacement, probably you will need to do following to find the node
在Ivan关于文本替换的回答之外,您可能需要执行以下操作来查找节点
XmlDocument xDoc = new XmlDocument();
xDoc.Load("yourXml.XML");
XmlNode nodetoChange = xDoc.DocumentElement.SelectSingleNode("step2");
string xmlValue = nodetoChange.InnerText;
//Now apply Ivan's logic to replace text as you wish so that xmlValue new text
//then continue to save xml as shown below.
nodetoChange.InnterText = xmlValue;
xDoc.Save(); //In case you need to save it on hard-disk.
xDoc = null;
Hope that helps, Thanks Milind
希望这能有所帮助,谢谢Milind