我创建了一个模板xml文件,其中包含一些像{contentname}这样的单词。 我需要用我的值替换这样的标签。 请告诉我如何在vb.net中搜索这样的单词并替换使用filehandling 我的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>
答案 0 :(得分:1)
对于类似的东西,如果文件很小且基于文本,我会使用正则表达式Replace,或更简单的String.Replace。
答案 1 :(得分:1)
如果您有一个有效的XML文件作为模板,则应遵循以下两种方法之一:
XmlDocument
并通过DOM 下面我说的是第一种方法。我将编写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 ...
答案 2 :(得分: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"))
答案 3 :(得分:0)
您可以使用String.Replace。