foydalanish:
public static T Getdasturning kodi(string body){
body.XmlToObject();
}
internal static class XmlJsonExt
{
public static T Deserialize(this string data)
{
return JsonConvert.DeserializeObject(data);
}
public static T XmlToObject(this string data)
{
return CreateDocument(data).XmlToJson().Deserialize();
}
#region Utils
private static XmlDocument CreateDocument(string body)
{
var document = new XmlDocument();
document.InnerXml = body;
return document;
}
private static string XmlToJson(this XmlDocument xmlDoc)
{
var sbJson = new StringBuilder();
sbJson.Append("{ ");
XmlToJsoNnode(sbJson, xmlDoc.DocumentElement, true);
sbJson.Append("}");
return sbJson.ToString();
}
// XmlToJSONnode: Output an XmlElement, possibly as part of a higher array
private static void XmlToJsoNnode(StringBuilder sbJson, XmlElement node, bool showNodeName)
{
if (showNodeName)
sbJson.Append("\"" + SafeJson(node.Name) + "\": ");
sbJson.Append("{");
// Build a sorted list of key-value pairs
// where key is case-sensitive nodeName
// value is an ArrayList of string or XmlElement
// so that we know whether the nodeName is an array or not.
var childNodeNames = new SortedList();
// Add in all node attributes
if (node.Attributes != null)
foreach (XmlAttribute attr in node.Attributes)
StoreChildNode(childNodeNames, attr.Name, attr.InnerText);
// Add in all nodes
foreach (XmlNode cnode in node.ChildNodes)
{
if (cnode is XmlText)
StoreChildNode(childNodeNames, "value", cnode.InnerText);
else if (cnode is XmlElement)
StoreChildNode(childNodeNames, cnode.Name, cnode);
}
// Now output all stored info
foreach (string childname in childNodeNames.Keys)
{
var alChild = (ArrayList) childNodeNames[childname];
if (alChild.Count == 1)
OutputNode(childname, alChild[0], sbJson, true);
else
{
sbJson.Append(" \"" + SafeJson(childname) + "\": [ ");
foreach (var child in alChild)
OutputNode(childname, child, sbJson, false);
sbJson.Remove(sbJson.Length - 2, 2);
sbJson.Append(" ], ");
}
}
sbJson.Remove(sbJson.Length - 2, 2);
sbJson.Append(" }");
}
// StoreChildNode: Store data associated with each nodeName
// so that we know whether the nodeName is an array or not.
private static void StoreChildNode(SortedList childNodeNames, string nodeName, object nodeValue)
{
// Pre-process contraction of XmlElement-s
if (nodeValue is XmlElement)
{
// Convertinto "aa":null
//xx into "aa":"xx"
var cnode = (XmlNode) nodeValue;
if (cnode.Attributes.Count == 0)
{
var children = cnode.ChildNodes;
if (children.Count == 0)
nodeValue = null;
else if (children.Count == 1 && (children[0] is XmlText))
nodeValue = ((XmlText) (children[0])).InnerText;
}
}
// Add nodeValue to ArrayList associated with each nodeName
// If nodeName doesn't exist then add it
var oValuesAl = childNodeNames[nodeName];
ArrayList valuesAl;
if (oValuesAl == null)
{
valuesAl = new ArrayList();
childNodeNames[nodeName] = valuesAl;
}
else
valuesAl = (ArrayList) oValuesAl;
valuesAl.Add(nodeValue);
}
private static void OutputNode(string childname, object alChild, StringBuilder sbJson, bool showNodeName)
{
if (alChild == null)
{
if (showNodeName)
sbJson.Append("\"" + SafeJson(childname) + "\": ");
sbJson.Append("null");
}
else if (alChild is string)
{
if (showNodeName)
sbJson.Append("\"" + SafeJson(childname) + "\": ");
var sChild = (string) alChild;
sChild = sChild.Trim();
sbJson.Append("\"" + SafeJson(sChild) + "\"");
}
else
XmlToJsoNnode(sbJson, (XmlElement) alChild, showNodeName);
sbJson.Append(", ");
}
// Make a string safe for JSON
private static string SafeJson(string sIn)
{
var sbOut = new StringBuilder(sIn.Length);
foreach (var ch in sIn)
{
if (Char.IsControl(ch) || ch == '\'')
{
int ich = ch;
sbOut.Append(@"\u" + ich.ToString("x4"));
continue;
}
if (ch == '\"' || ch == '\\' || ch == '/')
{
sbOut.Append('\\');
}
sbOut.Append(ch);
}
return sbOut.ToString();
}
#endregion
}
No comments:
Post a Comment