简体   繁体   English

在XML属性ID中查找字符串的一部分,并在C#中替换部分

[英]Find part of a string in XML attribute id and replace part in C#

I've the following simple XML document: 我有以下简单的XML文档:

<?xml version="1.0" encoding="utf-8"?>
<map title="enter map title here" id="_6cb8995f-430f-4422-918b-3042f6eda092">
  <topicref navtitle="Source" format="dita" id="_c2a73e14-2bbc-4730-88dc-0c4c699bd66e" scope="local" href="urn:ditastudio:topicreference:a2cec9de-d8e4-413e-ab1e-165a70c68adc">
    <topicref navtitle="Reference" format="dita" id="_18a0b17b-e5f0-4314-8536-d8152399f8d6" scope="local" href="urn:ditastudio:topicreference:7d25c6cc-b195-4968-af91-cd0d269f803b" />
  </topicref>
</map>

I want to use XPath to change part of the href in map//topicref/@href" . I want to keep the urn:ditastudio:topicreference: part and replace everything after it with with a random string. 我想使用XPath来更改map//topicref/@href" 。我想保留urn:ditastudio:topicreference: part并将其后的所有内容替换为随机字符串。

How do I do this in C#? 如何在C#中执行此操作?

So far this is what I'm doing.... 到目前为止,这就是我正在做的...。

foreach (Object map in mapList)
{
  XmlDocument theMap = new XmlDocument();
  theMap.XmlResolver = null;
  theMap.Load(map.ToString());

  String hrefPreText = "urn:ditastudio:topicreference:";
  String xPath = "/map//topicref/@href";


  foreach (string oldKey in OldToNewID_Dict.Keys)
  {
    String newVal = OldToNewID_Dict[oldKey];
    String newID = hrefPreText + newVal;

    XmlNode node = theMap.SelectSingleNode(xPath);
    node.Attributes[0].Value = newID;

    theMap.Save(map.ToString());
  }
}

I can't get my head around it. 我无法解决这个问题。 Any help would be appriciated! 任何帮助都将被申请!

Assuming you only want to do this for map/topicref , and not map/topicref/topicref too (in which case use : XElement.Descendants("topicref") ): 假设您只想对map/topicref执行此map/topicref ,而不要对map/topicref/topicref也进行此操作(在这种情况下,请使用: XElement.Descendants("topicref") ):

using System.Xml.XPath;
using System.Xml.Linq;

var xdocString = @"<?xml version="1.0" encoding="utf-8"?>
                    <map title="enter map title here" id="_6cb8995f-430f-4422-918b-3042f6eda092">
                      <topicref navtitle="Source" format="dita" id="_c2a73e14-2bbc-4730-88dc-0c4c699bd66e" scope="local" href="urn:ditastudio:topicreference:a2cec9de-d8e4-413e-ab1e-165a70c68adc">
                        <topicref navtitle="Reference" format="dita" id="_18a0b17b-e5f0-4314-8536-d8152399f8d6" scope="local" href="urn:ditastudio:topicreference:7d25c6cc-b195-4968-af91-cd0d269f803b" />
                      </topicref>
                    </map>";

XDocument xdoc = XDocument.Parse(xdocString);
String hrefPreText = "urn:ditastudio:topicreference:";

foreach (var topicref in xdoc.XPathSelectElements("map/topicref"))
{
    topicref.Attribute("href").Value = hrefPreText + "something random";
}

If you want to try this code, make sure to escape the " (replace with "" ) in xdocString , it messed up the formatting if I did it here. 如果您想尝试此代码,请确保在xdocString转义" (用""替换),如果我在此处这样做会弄乱格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM