I am trying to convert XML string to C# object, I have json string acle in xml tag, as shown below,
<message> <data:gcm xmlns:data=\"google:mobile:data\">{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}</data:gcm> </message>
I want json string from data tag I just want this string from above xml,
{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}
So how can I get this using c#.?
Thank you in advance.
By reading some LINQ to XML documents I got the solution which is like below,
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(msg.ToString());
var result = xdoc.Element("message").Descendants();
var myString = result.FirstOrDefault().Value; //This will out given json string
Again Thank you #JonSkeet for your suggestion.!
Related
Ok, I have an xml document which is returned via a c# function and output via XSLT. In the XML document there is an encrypted string which I need to decrypt before output.
Basically, i need to run "encryptedstring" through a decrypting function in order get the real value.
XML Structure:
<root>
<order encryptedstring="676696969hjhig">
</root>
Outputting function:
XmlDocument xmlOrders = Data_Functions.Get_All_Orders();
xmlOutput.DocumentContent = xmlOrders.OuterXml;
I am assuming i need to loop through the XML document, get the value for each "encryptedstring", run that value through the decrypt function and re-inject it back into the xml document but am unsure of best way to go about that.
The decryption has to be done in the codebehind in c# by passing a string through decryptString();
With a bit of guidance from the question suggestion by #momar this was solved in the following way...
XmlNodeList aNodes = xmlOrders.SelectNodes("//root/order");
foreach (XmlNode aNode in aNodes)
{
XmlAttribute ccAttribute = aNode.Attributes["encryptedstring"];
string attValue= ccAttribute.Value;
string encKey = ConfigurationManager.AppSettings["EncryptionKey"];
string decrypt = Crypto.DecryptStringAES(attValue, encKey);
ccAttribute.Value = deencrypt;
}
xmlOutput.DocumentContent = xmlOrders.OuterXml;
I want to convert a XML file to BSON. then import the BSON to MongoDB. I searched but could not find how to covert this using C#. please provide me a source code to do this using C#
Had the same Problem today.
It's for sure not the best solution, but
i solved it this way in my project and it works for what i need it:
Deserialize XML to Json
Deserialize Json to Bson
using (var reader = new StreamReader(context.Request.Body))
{
var body = reader.ReadToEnd(); // read input string
XmlDocument doc = new XmlDocument();
doc.LoadXml(body); // String to XML Document
string jsonText = JsonConvert.SerializeXmlNode(doc); //XML to Json
var bsdocument = BsonSerializer.Deserialize<BsonDocument>(jsonText); //Deserialize JSON String to BSon Document
var mcollection = Program._database.GetCollection<BsonDocument>("test_collection_05");
await mcollection.InsertOneAsync(bsdocument); //Insert into mongoDB
}
Below is code to convert xml to json using http://json.codeplex.com/
how to exclude null from JSON? (ie "SessionId": "null")
string xml = ""; //see XML value below
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc); //See Json value below
Xml Input
<MyResponse>
<Timestamp>2012-01-07T12:43:29</Timestamp>
<SessionId></SessionId>
</MyResponse>
Json Output
{"MyResponse":{"Timestamp":"2012-01-07T12:43:29","SessionId":null}}
You could have a simple string replace since you are outputting the JSON as a string. Do something like this:
jsonText = jsonText.Replace("null", "\"\"");
That should replace every occurrence of null with "".
It is not giving null property like this. It gives like nil to true as attribute in xml element.
string json = "{"Animal":{"id":"123","verified":true}}"
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
returnXml = doc.ToString();
Why does "ReturnXml" return the following text "System.Xml.XmlDocument" and not the XML output in string format?
http://json.codeplex.com/
To print XML, you need to use InnerXml
doc.InnerXml;
The ToString method of XmlDocument is not set to output a pretty version of the xml contained therein.
You're best bet may be to just convert that XmlDocument to an XDocument, since that supports a ToString method that outputs actual XML:
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
XDocument linqXML = XDocument.Load(new XmlNodeReader(doc));
returnXML = linqXML.ToString();
I have a Xdocument that is populated as follows:
XDocument xDoc = XDocument.Parse(new StreamReader(response.GetResponseStream()).ReadToEnd());
This gives me an XDocument that looks as follows:
<GetReportAsXMLString>
<report>
<reportItem Count ="562..................
</GetReportAsXMLStringResult>
Anything between the tag is all just a giant string(Black). How would I get this portion of the document to format as XML? The tag is also part of the string. I just don't know how to make this show it as not XML.
Thanks
It's hard to say from your description, but it looks like you'll need to first parse the response stream (valid xml), which contains another xml document (as a string). You'll need to extract the string from the 'outer' xml document and parse it into a new one:
psuedocode:
XDocument outer = response.GetResponseStream();
String innerXml = outer.Element("report").Value;
XDocument inner = XDocument.Parse(innerXml);
You have this XML content :
<GetReportAsXMLString>
<report>
<reportItem Count =\"562\"/>
<reportItem Count =\"562\"/>
</report>
</GetReportAsXMLString>
and you want to extract only the "reportItem" nodes?
If so you can do this:
string xml = "<GetReportAsXMLString><report><reportItem Count =\"562\"/><reportItem Count =\"562\"/></report></GetReportAsXMLString>";
XDocument xDoc = XDocument.Parse(xml);
IEnumerable<XElement> elList = xDoc.Descendants().Where(x => x.Name.LocalName.Equals("report")).Descendants().Where(x => x.Name.LocalName.Equals("reportItem"));