Thursday, November 3, 2016
Thursday, May 5, 2016
Custome Xml of SOAP service asmx
Follow this article
public class PrefixExtension : SoapExtension
{
// Fields
private Stream newStream;
private Stream oldStream;
private void AddPrefix()
{
this.newStream.Position = 0L;
this.newStream = this.ProcessXML(this.newStream);
this.Copy(this.newStream, this.oldStream);
}
public MemoryStream ProcessXML(Stream streamToPrefix)
{
streamToPrefix.Position = 0L;
XmlTextReader reader = new XmlTextReader(streamToPrefix);
XmlWriterSettings settings = new XmlWriterSettings();
// This is where the magic happens. I'm removing some of the default namespaces then adding soapenv instead of regular soap.
// There are many other things you can do once you get the response into the xmldocument object.
// After you are done it converts it back then writes it to the response.
XmlDocument doc = new XmlDocument();
doc.Load(reader);
doc.DocumentElement.Prefix = "SOAP-ENV";
//doc.DocumentElement.RemoveAttribute("xmlns:soap");
//doc.DocumentElement.RemoveAttribute("xmlns:xsi");
doc.DocumentElement.FirstChild.Prefix = "SOAP-ENV";
doc.DocumentElement.FirstChild.FirstChild.Prefix = "ns1";
XmlReader reader2 = new XmlNodeReader(doc);
settings.Encoding = Encoding.UTF8;
MemoryStream outStream = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(outStream, settings))
{
do
{
writer.WriteNode(reader2, true);
}
while (reader2.Read());
writer.Flush();
}
outStream.Seek(0, SeekOrigin.Begin);
return outStream;
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
case SoapMessageStage.AfterDeserialize:
return;
case SoapMessageStage.AfterSerialize:
this.AddPrefix();
return;
case SoapMessageStage.BeforeDeserialize:
this.GetReady();
return;
}
throw new Exception("invalid stage");
}
public override Stream ChainStream(Stream stream)
{
this.oldStream = stream;
this.newStream = new MemoryStream();
return this.newStream;
}
private void GetReady()
{
this.Copy(this.oldStream, this.newStream);
this.newStream.Position = 0L;
}
private void Copy(Stream from, Stream to)
{
TextReader reader = new StreamReader(from);
TextWriter writer = new StreamWriter(to);
writer.WriteLine(reader.ReadToEnd());
writer.Flush();
}
public override object GetInitializer(Type t)
{
return typeof(PrefixExtension);
}
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return attribute;
}
public override void Initialize(object initializer)
{
//You'd usually get the attribute here and pull whatever you need off it.
PrefixAttribute attr = initializer as PrefixAttribute;
}
[AttributeUsage(AttributeTargets.Method)]
public class PrefixAttribute : SoapExtensionAttribute
{
// Fields
private int priority;
// Properties
public override Type ExtensionType
{
get { return typeof(PrefixExtension); }
}
public override int Priority
{
get { return this.priority; }
set { this.priority = value; }
}
}
}
<configuration>
<system.web>
<webServices>
<soapExtensionTypes>
<add type="MvcApplication1.PrefixExtension,MvcApplication1" priority="1"/>
</soapExtensionTypes>
</webServices>
[PrefixExtension.PrefixAttribute]
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
public class PrefixExtension : SoapExtension
{
// Fields
private Stream newStream;
private Stream oldStream;
private void AddPrefix()
{
this.newStream.Position = 0L;
this.newStream = this.ProcessXML(this.newStream);
this.Copy(this.newStream, this.oldStream);
}
public MemoryStream ProcessXML(Stream streamToPrefix)
{
streamToPrefix.Position = 0L;
XmlTextReader reader = new XmlTextReader(streamToPrefix);
XmlWriterSettings settings = new XmlWriterSettings();
// This is where the magic happens. I'm removing some of the default namespaces then adding soapenv instead of regular soap.
// There are many other things you can do once you get the response into the xmldocument object.
// After you are done it converts it back then writes it to the response.
XmlDocument doc = new XmlDocument();
doc.Load(reader);
doc.DocumentElement.Prefix = "SOAP-ENV";
//doc.DocumentElement.RemoveAttribute("xmlns:soap");
//doc.DocumentElement.RemoveAttribute("xmlns:xsi");
doc.DocumentElement.FirstChild.Prefix = "SOAP-ENV";
doc.DocumentElement.FirstChild.FirstChild.Prefix = "ns1";
XmlReader reader2 = new XmlNodeReader(doc);
settings.Encoding = Encoding.UTF8;
MemoryStream outStream = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(outStream, settings))
{
do
{
writer.WriteNode(reader2, true);
}
while (reader2.Read());
writer.Flush();
}
outStream.Seek(0, SeekOrigin.Begin);
return outStream;
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
case SoapMessageStage.AfterDeserialize:
return;
case SoapMessageStage.AfterSerialize:
this.AddPrefix();
return;
case SoapMessageStage.BeforeDeserialize:
this.GetReady();
return;
}
throw new Exception("invalid stage");
}
public override Stream ChainStream(Stream stream)
{
this.oldStream = stream;
this.newStream = new MemoryStream();
return this.newStream;
}
private void GetReady()
{
this.Copy(this.oldStream, this.newStream);
this.newStream.Position = 0L;
}
private void Copy(Stream from, Stream to)
{
TextReader reader = new StreamReader(from);
TextWriter writer = new StreamWriter(to);
writer.WriteLine(reader.ReadToEnd());
writer.Flush();
}
public override object GetInitializer(Type t)
{
return typeof(PrefixExtension);
}
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return attribute;
}
public override void Initialize(object initializer)
{
//You'd usually get the attribute here and pull whatever you need off it.
PrefixAttribute attr = initializer as PrefixAttribute;
}
[AttributeUsage(AttributeTargets.Method)]
public class PrefixAttribute : SoapExtensionAttribute
{
// Fields
private int priority;
// Properties
public override Type ExtensionType
{
get { return typeof(PrefixExtension); }
}
public override int Priority
{
get { return this.priority; }
set { this.priority = value; }
}
}
}
<configuration>
<system.web>
<webServices>
<soapExtensionTypes>
<add type="MvcApplication1.PrefixExtension,MvcApplication1" priority="1"/>
</soapExtensionTypes>
</webServices>
[PrefixExtension.PrefixAttribute]
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
Sunday, November 8, 2015
Vb6 for Excel Cheat Sheet
Ngoài các hàm mặc định của VB6, còn có các hàm dành riêng cho Excel. Tham khảo 1 số ví dụ.
Các kiểu dữ liệu dành riêng cho Excel:
- Workbook
- Workbooks
- WorkSheet
- WorkSheets
- Range: 1 cell cũng là 1 Range
1. Set giá trị cho Cell A1 trong worksheet "Sheet1"
Range("'Sheet1'!A1").Value = "giá trị"
2. Set FontSize: Range("'Sheet1'!A1").Font.Size = 30
3. Gạch giữa chữ: Range("'Sheet1'!A1").Font.StrikeThrought = True
Các kiểu dữ liệu dành riêng cho Excel:
- Workbook
- Workbooks
- WorkSheet
- WorkSheets
- Range: 1 cell cũng là 1 Range
1. Set giá trị cho Cell A1 trong worksheet "Sheet1"
Range("'Sheet1'!A1").Value = "giá trị"
2. Set FontSize: Range("'Sheet1'!A1").Font.Size = 30
3. Gạch giữa chữ: Range("'Sheet1'!A1").Font.StrikeThrought = True
Macro in Excel
1. Enable Developer Toolbar (if not yet): Home -> Right click ribbon toolbar -> Customize Ribbon -> Check Developer checkbox.
2. Developer toolbar -> Macro -> Test this code:
2. Developer toolbar -> Macro -> Test this code:
Sub test()
Dim r As Range, c As Range
Dim x As String, j As Integer, k As Integer
Dim cfind As Range, r1 As Range
With Workbooks("excel A.xls").Worksheets("sheet1")
Set r = Range(.Range("A2"), .Range("A2").End(xlDown))
For Each c In r
x = c.Value
With Workbooks("excel B.xls")
j = .Worksheets.Count
For k = 1 To j
With .Worksheets(k)
Set cfind = .Cells.Find(what:=x, lookat:=xlWhole)
If Not cfind Is Nothing Then
Set r1 = Range(cfind.Offset(0, 1), cfind.End(xlToRight))
r1.Copy
GoTo pasting
End If
End With 'worksheets(k)
Next k
Exit Sub
End With 'second book
pasting:
c.Offset(0, 3).PasteSpecial
Next c
End With 'first book
End Sub
Thursday, October 29, 2015
Một số lưu ý khi sử dụng Json.NET
1. Create Json Model Object: sử dụng JsonProperty để định nghĩa tên của 1 field.
2. Khi parse json string. Nên parse thành JToken, vì nó tổng quan nhất, sau đó dựa vào cái type của nó để xác định xem nên parse thành gì (nếu không có thể sẽ bị exception). Điều này quan trọng để parse string thành 1 type nào đó, đôi lúc string không hợp lệ cho type đó.
2. Khi parse json string. Nên parse thành JToken, vì nó tổng quan nhất, sau đó dựa vào cái type của nó để xác định xem nên parse thành gì (nếu không có thể sẽ bị exception). Điều này quan trọng để parse string thành 1 type nào đó, đôi lúc string không hợp lệ cho type đó.
Tuesday, October 20, 2015
How to detect error base on RestSharp returned response fields?
Never check ErrorMessage for detecting error, just check StatusCode.
Note. This picture is In case of using Asp Web Api 2, doesn't has ErrorMessage but has System.Net.HttpStatusCode.NotFound status. (See all http status codes)
Một số lưu ý khi sử dụng SQLite
Links hay:
http://blogs.msdn.com/b/matt/archive/2008/05/22/into-to-linq-to-sql-optimistic-concurrency.aspx
1. Khi tạo các model, sử dụng namespace using System.Data.Linq.Mapping;
Example:
2. Không sử dụng 1 số hàm sau trên IQueryable (của System.Linq):
http://blogs.msdn.com/b/matt/archive/2008/05/22/into-to-linq-to-sql-optimistic-concurrency.aspx
1. Khi tạo các model, sử dụng namespace using System.Data.Linq.Mapping;
Example:
[Table(Name = "McmUser")]public class McmUser{[Column(Name = "Username", IsPrimaryKey = true)]public string Username { get; set; }[Column(Name = "IsActive")]public bool IsActive { get; set; }}
2. Không sử dụng 1 số hàm sau trên IQueryable (của System.Linq):
- First
- FirstOrDefault
- ElementAt
Subscribe to:
Posts (Atom)