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

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 に答える
1

このような場合、ファイルが小さくテキスト ベースの場合は、正規表現Replaceまたはより単純な String.Replace を使用します。

于 2009-12-30T08:04:19.210 に答える
1

テンプレートとして有効な XML ファイルがある場合は、次の 2 つの方法のいずれかに従う必要があります。

  • として開き、XmlDocumentDOM を介して値を更新します
  • 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 に答える
0

String.Replaceを使用できます。

于 2009-12-30T08:05:23.483 に答える