-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamlQueryApiCore.cs
More file actions
64 lines (60 loc) · 2.37 KB
/
Copy pathCamlQueryApiCore.cs
File metadata and controls
64 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Author: Antonio Leonardo de Abreu Freire, Microsoft Certified ID: 13271836
//My profile: https://br.linkedin.com/in/antonio-leonardo
//Contacts: antonio.leonardo@outlook.com.br
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.Serialization;
namespace CamlQueryApi
{
/// <summary>
/// Utilitary class to define internal methods, used only on API
/// </summary>
internal static class CamlQueryApiSerializer
{
/// <summary>
///
/// </summary>
private enum RootRemovible
{
Query,
ViewFields
}
/// <summary>
/// Generates a CAML Query String, based on POCO class API Xml Serialization
/// </summary>
/// <typeparam name="TEntity">CAML Query Entity Class</typeparam>
/// <param name="obj">Object based on Generic defined Entity class</param>
/// <returns>System.String</returns>
internal static string ToCamlString<TEntity>(this TEntity obj) where TEntity : class, new()
{
Type currentType = typeof(TEntity);
XmlSerializer serializer = null;
XmlWriterSettings settings = null;
XmlSerializerNamespaces ns = null;
string className = currentType.Name;
const string BEGIN_BEGIN_TAG = "<",
BEGIN_END_TAG = "</",
END_TAG = ">";
using (StringWriter stringWriter = new StringWriter())
{
settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.Encoding = Encoding.UTF8;
using (XmlWriter xmlTxtWriter = XmlTextWriter.Create(stringWriter, settings))
{
serializer = new XmlSerializer(currentType);
ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(xmlTxtWriter, obj, ns);
return (className == RootRemovible.Query.ToString() || className == RootRemovible.ViewFields.ToString()) ?
@stringWriter.ToString().Replace(BEGIN_BEGIN_TAG + className + END_TAG, "").Replace(BEGIN_END_TAG + className + END_TAG, "") :
@stringWriter.ToString();
}
}
}
}
}