0

我创建了一个包含 {contentname} 之类的词的模板 xml 文件。我需要用我的值替换这样的标签。请告诉我如何在 vb.net 中搜索这样的单词并使用文件处理替换我的 xml 模板文件是这样的:

<!-- BEGIN: main -->
<?xml version="1.0" encoding="UTF-8"?>
<OTA_HotelSearchRQ xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05OTA_HotelSearchRQ.xsd" EchoToken="{EchoToken}" Target="{Target}" Version="1.006" PrimaryLangID="{PrimaryLangId}" MaxResponses="{MaxResponses}">
<POS>
<!-- BEGIN:Source -->
<Source>
<RequestorID ID="{affiliateId}" MessagePassword="{MessagePassword}" />
</Source>
<!-- END:Source -->
</POS>
<Criteria <!-- BEGIN:AvailableOnlyIndicator -->AvailableOnlyIndicator="    {AvailableOnlyIndicator}"<!-- END:AvailableOnlyIndicator -->>
<Criterion>

4

4 回答 4

1

对于这样的事情,如果文件很小并且基于文本,我会使用正则表达式Replace或更简单的 String.Replace。

于 2009-12-30T08:04:19.210 回答
1

如果您有一个有效的 XML 文件作为模板,您应该遵循以下两种方法之一:

  • 打开它XmlDocument并通过 DOM 更新您的值
  • 创建 XSLT 并传递参数以转换模板

下面我说的是第一种方法。我将编写 C#,但您可以轻松地将其转换为 VB.NET:

XmlDocument doc = new XmlDocument();
doc.Load("yourfile.xml");

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ota", "http://www.opentravel.org/OTA/2003/05")

XmlElement hotelSearch = doc.SelectSingleNode
    ("/ota:OTA_HotelSearchRQ", nsmgr) as XmlElement;
hotelSearch.SetAttribute("EchoToken", "{EchoToken}");
hotelSearch.SetAttribute("Target", "{Target}");
// ... and so on ...

XmlElement requestorId = hotelSearch.SelectSingleNode
    ("ota:POS/ota:Source/ota:RequestorID", nsmgr) as XmlElement;
requestorId.SetAttribute("ID", "{affiliateId}");
requestorId.SetAttribute("MessagePassword", "{MessagePassword}");
// ... and so on ...
于 2009-12-30T09:31:45.213 回答
1

Rubens Farias 先生提供的解决方案的 VB.NET 版本(如果有人需要。它的工作):

Dim doc As XmlDocument = New XmlDocument()
    doc.Load(HttpContext.Current.Server.MapPath("~\actions\HOTEL_SEARCH.template.xml"))

    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
    nsmgr.AddNamespace("ota", "http://www.opentravel.org/OTA/2003/05")

    Dim hotelSearch As XmlElement = CType(doc.SelectSingleNode("/ota:OTA_HotelSearchRQ", nsmgr), XmlElement)
    hotelSearch.SetAttribute("EchoToken", BLLHotel_Search.EchoToken)
    hotelSearch.SetAttribute("Target", BLLHotel_Search.Target)

    Dim requestorId As XmlElement = CType(hotelSearch.SelectSingleNode("ota:POS/ota:Source/ota:RequestorID", nsmgr), XmlElement)
    hotelSearch.SetAttribute("ID", BLLHotel_Search.affiliateId)
    hotelSearch.SetAttribute("MessagePassword", BLLHotel_Search.MessagePassword)

    doc.Save(HttpContext.Current.Server.MapPath("hello.xml"))
于 2009-12-30T10:06:17.843 回答
0

您可以使用String.Replace

于 2009-12-30T08:05:23.483 回答