Reputation: 3896
I have some strings in this format:
public static string Script(string user, string password)
{
return @"<?xml version='1.0'?>"
+ "<RIBCL VERSION='2.0'>"
+ "<LOGIN USER_LOGIN='" + user + "' PASSWORD='" + password + "'>"
+ "<SERVER_INFO MODE='read'><GET_HOST_DATA /></SERVER_INFO>"
+ "<RIB_INFO MODE='read'><GET_NETWORK_SETTINGS/></RIB_INFO>"
+ "</LOGIN>"
+ "</RIBCL>";
}
How can I get the above string variable into an xml file that looks like this:
<?xml version='1.0'?>"
<RIBCL VERSION='2.0'>
<LOGIN USER_LOGIN="user" PASSWORD="password">
<SERVER_INFO MODE="read">
<GET_HOST_DATA />
</SERVER_INFO>"
<RIB_INFO MODE="read">
<GET_NETWORK_SETTINGS/>
</RIB_INFO>
</LOGIN>
</RIBCL>
Where user
and password
need to be actual string variables.
Instead of having one different method for every string(quite a few) can I somehow store each into their own file? The only thing would be the user
and password
variables which would need to get added everytime so not sure how to handle that part.
Upvotes: 0
Views: 817
Reputation: 1
I have had to create a similar application to be called from ASP.NET. The solution I found the best was to create the xml file in resources folder and just update the USER_LOGIN and PASSWORD attributes.
One other point to note is that you need to remove the xml declation from the top when sending to HPILO as it does not recognise it. The file should start with
<RIBCL VERSION=2.0>
Upvotes: 0
Reputation: 900
Try to store your string like string format template
For example, like:
<?xml version='1.0'?>
<RIBCL VERSION='2.0'>
<LOGIN USER_LOGIN='{0}' PASSWORD='{1}'>
<SERVER_INFO MODE='read'><GET_HOST_DATA /></SERVER_INFO>
<RIB_INFO MODE='read'><GET_NETWORK_SETTINGS/></RIB_INFO>
</LOGIN>
</RIBCL>
Your script method should load particular string from somewhere, and after that just should apply String.Format
:
return String.Format(data, HttpUtility.HtmlEncode(user), HttpUtility.HtmlEncode(password));
It will work if each of your strings has only user
and password
fields, which should be set.
Upvotes: 2
Reputation: 161831
First, never build XML using string manipulation. The rules are different. For instance, what if password
had a character in it that was invalid for an xml attribute?
Second, I would recommend building your XML using LINQ to XML. No file I/O that way.
Upvotes: 1