6
Answers

How to write queriestring inside the xml file and how to replace the value of a string in the querystring


Hi All,
I have a function call in file1.aspx.cs where I am passing two string values as below.
string strQuery = GlobalQuery.GetQuery(seccondSelection, prefixText);
and inside the GlobalQuery class I have the following code for the static function GetQuery.
public static string GetQuery(string strType, string strLikeChar)
{
  strAT = "sample 1";
  strMK = "sample 2";
  strSN = "sample 3";
 
  string strReturn = string.Empty;
  string strDynamic = string.Empty;
  strDynamic = "Dynamic";
  if ((strLikeChar != "") && (strType != "Aircraft")) strDynamic = strType + "_" + strDynamic;
  else strDynamic = strType;
  dynamicString = strLikeChar.ToUpper(); //first change
  switch (strDynamic)
 {
  case "Aircraft_Dynamic":
  strReturn = "SELECT  h.FUNCTION_NO, Eng_Function_API.Get_Description(FUNCTION_NO) Func_desc "
  + " FROM test_table h "
  + "FROM test_table h  WHERE h.PRODUCT_NO = '" +
strAT + "'"
  + " AND h.MODEL_NO = '" +
strMK + "'"
  + " UPPER(Eng_Function_API.Get_Description(FUNCTION_NO)) LIKE '" + dynamicString + "%'"
  + " and h.parent_function_no is null ";

  break;
  case "Aircraft":
  strReturn = "SELECT  h.FUNCTION_NO,Eng_Function_API.Get_Description(FUNCTION_NO) Func_desc "
  + "FROM test_table h  WHERE h.PRODUCT_NO = '" +
strAT + "'"
  + " AND h.MODEL_NO = '" +
strMK + "'"
  + " and h.parent_function_no is null ";

  break;
 // have 2 more case values which will retrieve different set of data.
 } 
}
Now I want know how to write the query part (highlited in red) in XMlL file and how to replace string values inside the query in XML file(highlited in blue)..
I also want to know how to get that querystring from the XML file
string strQuery = GlobalQuery.GetQuery(seccondSelection, prefixText); // GlobalQuery.GetQuery(seccondSelection, prefixText); should be replaced by the XML file which should contain the query part.
Thanks,  Sachi






Answers (6)
1
Vulpes

Vulpes

NA 98.3k 1.5m 13y
OK, I think I've grasped it now :)

 case "Aircraft":  
    XElement auto = XElement.Load("auto.xml");
    XElement aircraft =  auto.Elements().Where(element => element.Name == "aircraft").First();
    strReturn = aircraft.Attribute("command").Value;
    strReturn = strReturn.Replace("@strAT", "'" + strAT + "'");
    strReturn = strReturn.Replace("@strMK", "'" + strMK + "'");
    break;
Accepted
0
sachi vasishta

sachi vasishta

NA 277 0 13y
Thanks a lot Vulpes :):):)
0
sachi vasishta

sachi vasishta

NA 277 0 13y
Thanks Vulpes for your kind help. It's my fault that I didn't make my point clear.

This is what I have in my XML file(Auto.XML)

<root>
  <fault>
  <text>Pending Fault found on</text> 
  <text>Repaired Fault on</text>
  </fault>
  <genere>
  <text>Aircraft</text>
  <text>Component</text>
  <text>Subsystem</text>
  </genere>
  <aircraft command="SELECT  h.FUNCTION_NO,Eng_Function_API.Get_Description(FUNCTION_NO) Func_desc
  FROM hi_table h  WHERE h.PRODUCT_NO = @strAT AND h.MODEL_NO = @strMK and parent_function_no is null;"
  type="text">
 
  </aircraft> 
 
</root>


this is what I have in my codebehind file

public static string[] DBPopUp(string seccondSelection, string prefixText)
  {
  string[] arrReturn =null;
  string strType = seccondSelection;
  string strLikeChar = prefixText;
  string strAT = "sample1";
  string strMK = "sample2";
  string strSN = "sample3";
  string strReturn = string.Empty;
  string strDynamic = string.Empty;
  strDynamic = "Dynamic";
  if ((strLikeChar != "") && (strType != "Aircraft")) strDynamic = strType + "_" + strDynamic;
  else strDynamic = strType;
  dynamicString = strLikeChar.ToUpper(); 
  switch (strDynamic)
  {
 case "Aircraft_Dynamic":
  break;
  case "Aircraft":  strReturn =  // here I want to get the querystring written in the 'command' attribute of the 'aircraft' element of Auto.XML file and I also want to know how to replace @strAT and @strMK parameters
  break;
 // two more case statements will follow
  }
 
so the querystring returned from the XML should be like this
SELECT  h.FUNCTION_NO,IFSAPP.Eng_Function_API.Get_Description(FUNCTION_NO) Func_desc
FROM IFSAPP.PRODUCT_FUNCTION h  WHERE h.PRODUCT_NO = 'sample1'(value of @strAT) AND h.MODEL_NO = 'sample2'(value of @strMK) and parent_function_no is null;
strReturn is the final querystring I am going to pass to database.


0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
OK, what I'd suggest here is that, instead of sample 1, sample 2 etc, I'd use placeholders like you'd find in the String.Format or Console.WriteLine methods.

So within the GetQuery method you'd have:

  strAT = "{0}";
  strMK = "{1}";
  strSN = "{2)";

On retrieving the query string from the method, you can then replace the placeholders with whatever values you like before writing the string to the XML file.

So you'd have something like this:

string strQuery = GlobalQuery.GetQuery(seccondSelection, prefixText);
strQuery = String.Format(strQuery, "some AT value", "some MK value");
Xlement auto = XElement.Load("auto.xml");
XElement aircraft =  auto.Elements().Where(element => element.Name == "aircraft").First();
aircraft.Value = strQuery;
auto.Save("auto.xml");

To retrieve the query string from the XML file, you'd then need:

XElement auto = XElement.Load("auto.xml");
XElement aircraft =  auto.Elements().Where(element => element.Name == "aircraft").First();
string strQuery = aircraft.Value;
0
sachi vasishta

sachi vasishta

NA 277 0 13y

Hi Vulpes,

I want to write the querystring in an existing XML file(Auto.xml).

this is the one I am having

<root>
  <fault>
    <text>Pending Fault found on</text> 
    <text>Repaired Fault on</text>
  </fault>
  <genere>
    <text>Aircraft</text>
    <text>Component</text>
    <text>Subsystem</text>
  </genere>
   <aircraft>
       " I want to write the querystring here and also I want to know how to replace the string values(highlited in blue) "
   </aircraft>
</root>


Also please note that the XML file should retrun a querystring of type string.
I mean, in the below code

string strQuery = GlobalQuery.GetQuery(seccondSelection, prefixText);
GlobalQuery.GetQuery(seccondSelection, prefixText); // this returns a querystring, the same I want to achieve using XML(I want to pass those 2 string values to the XML and get the resultant querystring from XML and assign to strQuery).
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
Are you wanting to write the query string to an existing XML file or to a new one?

In either case, can you give a sample file and indicate under which node etc. the string is to be written.