how to exclude null from JSON? (after converting from XML) - c#

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.

Related

How to get deserialized xml attribute from dynamic object

I can get the element innertext from expandoobject without any problem. I can't figure out how to get the attribute's value.
By doing Console.WriteLine(obj.Message.Body), I can get the expected string inside the body element.
private void TestXML()
{
string xmlString = #"<?xml version=""1.0"" encoding=""utf-8""?><Message important=""yes"" recevied=""2019-2-12""><Body>Hi there fella!</Body></Message>";
XDocument doc = XDocument.Parse(xmlString);
string json = JsonConvert.SerializeXNode(doc);
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json);
Console.WriteLine(obj.Message);
}
I did a debug and and under obj.Message I can see 3 fields:
#important with value "yes"
#received with value "2019-2-12"
Body with value "Hi there fella!"
Is there a way to retrieve the first 2 fields' values with a # prefix? I have no idea how to deal with this # character on dynamic objects.
To deal with special characters, such as "#" in dynamic object, you must cast it to `
(IDictionary). And then you can get the recevied attribute as bellow:
var received = ((IDictionary<string, object>)obj.Message)["#recevied"];

How get JSON string from XML string using C#?

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.!

How get the attribute value of XML node in C#

I am parsing the XML in C# this XML:
<Resident Type="R">
<Payment>1218</Payment>
</Resident>
I am parsing this way(please answer this same way, not other methods)
XmlDocument parsed_xml = new XmlDocument();
parsed_xml.LoadXml(dto.xml);
XmlNodeList test = parsed_xml.SelectNodes("/IER/Credit/Loan/LoanApp/Applicant/Personal/Individuals/Individual/Resident/Peyment");
if (xnList != null)
PAYMENT = xnList.Item(0).InnerText;
with this code I can get the Payment value that is 1218 but how I can get the attribute value of Type that is "R" ?
You'll want to look at the ParentNode to get the attribute.
string residentType = xnList[0].ParentNode.Attributes["Type"].Value;

XML to JSON conversion accessing member in C#

I have an XML string like
<Segment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Air">
<carrier />
...
</Segment>
I use JSON.NET to convert that to JSON and the end result has the following members
#xmlns:xsi: "http://www.w3.org/2001/XMLSchema-instance"
#xsi:type: "Air"
carrier
At client end, how do I access the type "Air"? Segment.#xsi:type obviously is illegal.
I presume you use Json.Net at client end.
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xstr);
string jsonText = JsonConvert.SerializeXmlNode(xDoc);
JObject jObj = (JObject)JsonConvert.DeserializeObject(jsonText);
string air = jObj["Segment"]["#xsi:type"].ToString();
//or
dynamic jObj = JsonConvert.DeserializeObject(jsonText);
string air = jObj.Segment["#xsi:type"];

JSON.NET XML to String

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();

Categories

Resources