How To Copy All Nodes And Change The Value Of Certain Nodes



TODO:

Have you ever wanted to copy an XML document, but change certain node data during the copy?

 

SOLUTION:

 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 
  <!-- copy your input verbatim -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <!-- Now copy book id now appending the prefix to the data -->
  <xsl:template match="/Books/Book/BookID">
    <BookID>SomeValue-<xsl:apply-templates select="@*|node()"/></BookID>
  </xsl:template>

</xsl:stylesheet>

 

 

NOTES:

There are no notes on this topic.

How To Select Records X Minutes in Age



TODO:

Have you ever wanted to select records in a table that are a certain number of minutes in age?

 

SOLUTION:

 

select *
from mytable
where datecreated < (SELECT DATEADD(minute, -X, GETDATE()) )

 

 

NOTES:

In the example above replace X with the number of minutes in age.  i.e. to select records 30 minutes in age, replace X with 30, so it would be "datecreated < (SELECT DATEADD(minute, -30, GETDATE()) )"

How To POST SOAP To A .Net Web Service



TODO

Have you ever wanted to POST SOAP to a .Net Webservice?

 

SOLUTION:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://myservice.com/myservice.asmx");

String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\"><soapenv:Body>YOUR SOAP BODY HERE</soapenv:Body></soapenv:Envelope>";
ASCIIEncoding encoding = new ASCIIEncoding();

byte[] bytesToWrite = encoding.GetBytes(xmlString);
request.Method = "POST";
request.ContentLength = bytesToWrite.Length;
request.ContentType = "application/soap+xml; charset=UTF-8; action=\"http://myservice.com/MyMethod\"";
request.UserAgent = "Axis2";
request.Host = "myservice.com";
Stream newStream = request.GetRequestStream();
newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
newStream.Close();

try
{
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);

    string responseFromServer = reader.ReadToEnd();
    Console.WriteLine(responseFromServer);
}
catch (WebException ex)
{
    Console.WriteLine(ex);
    WebResponse errRsp = ex.Response;
    using (StreamReader rdr = new StreamReader(errRsp.GetResponseStream()))
    {
        Console.WriteLine(rdr.ReadToEnd());
    }
}

 

NOTES:

No notes apply to this topic.

How To Format Money / Decimal In XSLT



TODO:

Have you ever wanted to make a nice money string from a number in XSLT?

 

SOLUTION:

 

<xsl:variable name="MyMoney" select='format-number( round(100*$root/DollarString) div 100 ,"##0.00" )' />

 

NOTES:

This creates a nice formatted string in a variable, that can then be used later.

How To Test For NaN In XSLT



TODO:

Have you ever wanted to test for NaN in XSLT?

 

SOLUTION:

<xsl:variable name="My_Variable">
     <xsl:choose>
          <xsl:when test="string(number($root/mynode))='NaN'"></xsl:when>
     <xsl:otherwise>
           <xsl:value-of select="number(string($root/mynode))" />
     </xsl:otherwise>
     </xsl:choose>
</xsl:variable>

 

NOTES:

This snippet will test for NaN.  If found, it assigns empty string to the variable, and if it is not found it just uses the node value.  Then you just use this variable later and all is well.

How To Programatically Scroll To The Last Added Row Of A DataGridView



TODO:

Have you ever wanted to scroll to the last row of a DataGridView?

 

SOLUTION:

//add a row to the grid
dgvMyGrid.Rows.Add(rowdata);

//set the current cell, which will scroll to it
dgvMyGrid.CurrentCell = dgvMyGrid.Rows[dgvMyGrid.RowCount - 1].Cells[0];

 

NOTES:

There are no notes on this topic.

How To Compare Node To A String In XSLT



TODO:

Have you ever wanted to compare a node value to a string and use a new value when there is a match?

 

SOLUTION:

 

<xsl:variable name="newValue">
	<xsl:choose>
		<xsl:when test="string($myRoot/MyNode) = 'Some Value'">123456</xsl:when>
              	<xsl:otherwise>
                	<xsl:value-of select="string($myRoot/MyNode)"/>
              	</xsl:otherwise>
	</xsl:choose>
</xsl:variable>          

<MyDataNode>
	<xsl:value-of select="$newValue"/>
</MyDataNode>

 

NOTES:

The above checks the value of $myRoot.MyNode and assigns a value if there is a match.  If there is no match, the "otherwise" is reached and I use the value in the node.  I then use that value for MyDataNode.

There is an error in the document xmlns= was not expected



TODO:

Have you ever received the message "There is an error in the document (1,2) <MyElement xmlns=''> was not expected." when deserializing an XML string?

 

SOLUTION:

After much headache, it turned out that the person was sending me xml that had the proper tags, but had the incorrect case.  XML is case sensitive, so be sure to check that first, especially if it was working before.

 

NOTES:

There are no notes for this topic.

How To Use Regular Expressions To Find 2 Words With Any Number Of Spaces Between Them



TODO:

Have youever wanted to find 2 words when parsing data, remove them, even though there may be a varying number of spaces between the words?

 

SOLUTION:

//set our test input string
string data = "what is   thought     is 1. the data is";   //our test string

//Now match on "is thought is" phrase with any number of SPACES between words
Match match = Regex.Match(data, "is\\s{1,}thought\\s{1,}is");

//Display if it was OK
Console.WriteLine("Success: " + match.Success);

//Display the index
Console.WriteLine("Index: " + match.Index);

//Display the length of the matched string
Console.WriteLine("Length: " + match.Length);

//Now grab everything AFTER the matched string
Console.WriteLine("Trimmed: " + data.Substring(match.Index +match.Length));

 

 

NOTES:

The output is:

Success: True
Index: 5
Length: 19
Trimmed:  1. the data is

System.InvalidOperationException: Request format is invalid: multipart/form-data; boundary Error When Calling .Net Web Service From PHP



TODO:

Have you ever tried to use php/curl to call a .Net Webservice and received the following:

System.InvalidOperationException: 

Request format is invalid: multipart/form-data;

boundary=----------------------------7d78134d9ec2.
 at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
 at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

 

SOLUTION:

 

<?php
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_VERBOSE, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_URL,'http://my.webservice.com/MyService.asmx/MyMethod' );
	$post_array = array(
			"Param1"=>'xyz',
			"Param2"=>'abc',
			"Param3"=>'123'
	);

	//url-ify the data
	foreach($post_array as $key=>$value) 
	{ 
		$post_array_string .= $key.'='.$value.'&'; 
	}
	$post_array_string = rtrim($post_array_string,'&');
	//set the url, number of POST vars, POST data
	curl_setopt($ch,CURLOPT_POST,count($post_array ));
	curl_setopt($ch,CURLOPT_POSTFIELDS,$post_array_string);
	$response = curl_exec($ch);
	print_r($response);
?>

 

NOTES:

This happens when you have parameters in your web method.  If you have no parameters, you can just access the Form["Param1"] to get to the data and do not need this foreach code at the end.