0

i have created a template xml file witch contain some words like {contentname}. i need to replace such a tags with my values. please tell me how to search such a words and replace using filehandling in vb.net my xml templatefile is like this:

<!-- 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>

casperOne
  • 73,706
  • 19
  • 184
  • 253
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178

4 Answers4

1

For something like this, if the files are small and text based, I would use regular expression Replace, or the simpler String.Replace.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

If you have a valid XML file as template, you should follow one of two ways:

  • Open it as XmlDocument and update your values through DOM
  • Create a XSLT and pass your parameters to transform your template

Below I'm talking about first method. I'll write C#, but you can easily translate it to 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 ...
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • wow.. Rubens is always their to help me... u always gives perfect solutions. thanks a lot – Dr. Rajesh Rolen Dec 30 '09 at 09:35
  • please tell me how to set value of that Tag/element because i tried to do so but i am getting error: Cannot set a value on node type 'Element'. i have following code: Dim CountryName As XmlElement = CType(hotelSearch.SelectSingleNode("/ota:OTA_HotelSearchRQ/ota:Criteria/ota:Criterion/ota:Address/ota:CountryName", nsmgr), XmlElement) CountryName.SetAttribute("Code", BLLHotel_Search.CountryCode) CountryName.Value = BLLHotel_Search.CountryName – Dr. Rajesh Rolen Dec 30 '09 at 10:57
  • i got it thanks, its: CountryName.ChildNodes(0).InnerText = BLLHotel_Search.CountryName – Dr. Rajesh Rolen Dec 30 '09 at 11:09
1

VB.NET version (if someone requires. Its working) of solution provided by Mr. Rubens Farias:

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"))
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
0

You could use String.Replace.

Andy West
  • 12,302
  • 4
  • 34
  • 52