替换XML元素标记之间的字符串

时间:2014-05-15 14:32:00

标签: c# regex

我需要替换元素的值。元素和命名空间将是动态的。

我认为我的正则表达式有问题。

string key = "BusinessID";
Regex x = new Regex("(<" + key  + "(.*)" + "'>)(.*)(</" + key + ">)");
string s = @"<BusinessID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</BusinessID>";
string repl = "the replacement text";
string Result = x.Replace(s, "$1" + repl + "$3");

当前结果:

<BusinessID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>the replacement textstring

期望的结果:

<BusinessID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>the replacement text</BusinessID>

我怎样才能做到这一点?

针对更复杂的情况进行了扩展

我有List<KeyValuePair<string, object>>我需要使用object中的值更新XML。密钥将与XML元素对齐。

完整的XML:

<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
    <CreateQueuedMsg xmlns='http://tempuri.org/'>
        <Token xmlns='http://tempuri.org/'>string</Token>
        <BGSMSMessage>
            <BusinessID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</BusinessID>
            <CommsGUID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</CommsGUID>
            <DestinationAddress xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</DestinationAddress>
            <Msg xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</Msg>
            <MsgEncodingType xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</MsgEncodingType>
            <SendDT xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>dateTime</SendDT>
            <SystemID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</SystemID>
            <ValidityDT xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>dateTime</ValidityDT>
        </BGSMSMessage>
        <smsRoute>
            <SMSRoute xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</SMSRoute>
            <SMSRoute xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</SMSRoute>
        </smsRoute>
    </CreateQueuedMsg>
</soap:Body>
</soap:Envelope>

对象:

List<KeyValuePair<string, object>> lKVP = new List<KeyValuePair<string, object>>();

List<SMSRoute> smsRoute = new List<SMSRoute> { SMSRoute.BGWASP, SMSRoute.GrapeVine };
lKVP.Add(new KeyValuePair<string, object>("Token", "AD1518D9-4110-411E-11A5-762B14919797"));
lKVP.Add(new KeyValuePair<string, object>("BusinessID", BusinessID.Test));
lKVP.Add(new KeyValuePair<string, object>("CommsGUID", Guid.NewGuid().ToString()));
lKVP.Add(new KeyValuePair<string, object>("DestinationAddress", "0722222222"));
lKVP.Add(new KeyValuePair<string, object>("Msg", "Testers" + DateTime.Now.ToString()));
lKVP.Add(new KeyValuePair<string, object>("MsgEncodingType", BGSMSDataCodings.Default));
lKVP.Add(new KeyValuePair<string, object>("SendDT", DateTime.Now));
lKVP.Add(new KeyValuePair<string, object>("SystemID", SystemID.Test));
lKVP.Add(new KeyValuePair<string, object>("ValidityDT", DateTime.Now.AddDays(3)));
lKVP.Add(new KeyValuePair<string, object>("smsRoute", smsRoute));

3 个答案:

答案 0 :(得分:4)

您似乎使用了错误的工具,而是使用LINQ to XML,例如

var element = XElement.Parse("<BusinessID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</BusinessID>");
element.Value = "New value";
var xml = element.ToString();

假设您的问题中的代码实际上已经简化,这里是一个查询实际XML文档的更完整的示例

var xdoc = XDocument.Parse("...");
var xname = XName.Get("BusinessID", "http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes");
var businessId = xdoc.Descendants(xname).FirstOrDefault();
businessId.Value = "New value";
string result = xdoc.ToString(); 

答案 1 :(得分:2)

你有一些额外的括号,这有效:

string key = "BusinessID";
Regex x = new Regex("(<" + key + ".*" + "'>)(.*)(</" + key + ">)");
string s = @"<BusinessID xmlns='http://schemas.datacontract.org/2004/07/BG.Bus.Mobile.Classes'>string</BusinessID>";
string repl = "the replacement text";
string Result = x.Replace(s, "$1" + repl + "$3");

詹姆斯是对的,你应该使用LINQ to XML ...

答案 2 :(得分:1)

考虑以下内容......

var result = Regex.Replace(s, string.Format(@"(?<={0}.*?\>).*(?=<)", key), repl);

祝你好运!