C # code for converting JSON strings to XML objects

Source: Internet
Author: User

Xmldictionaryreader reader = jsonreaderwriterfactory. createjsonreader (encoding. utf8.getbytes (sjson), xmldictionaryreaderquotas. max );
Xmldocument Doc = new xmldocument ();
Doc. Load (Reader );

Add it to favorites.

I tried it. The JSON string needs to be key-added.
For example: {name: "AAA", value: 1} Here, name and value are keys, and an error is reported during conversion.
To be written
{"Name": "AAA", "value": 1}

 

 

This is the second method. If this key is not added, the translation is normal.

//


// convert a JSON string to an XML Object
///
//
//
Public static xmldocument json2xml (string sjson)
{< br> // xmldictionaryreader reader = jsonreaderwriterfactory. createjsonreader (encoding. utf8.getbytes (sjson), xmldictionaryreaderquotas. max);
// xmldocument Doc = new xmldocument ();
// Doc. load (Reader);

Javascriptserializer oserializer = new javascriptserializer ();
Dictionary <string, Object> DIC = (Dictionary <string, Object>) oserializer. deserializeobject (sjson );
Xmldocument Doc = new xmldocument ();
Xmldeclaration xmldec;
Xmldec = Doc. createxmldeclaration ("1.0", "gb2312", "yes ");
Doc. insertbefore (xmldec, Doc. documentelement );
Xmlelement nroot = Doc. createelement ("root ");
Doc. appendchild (nroot );
Foreach (keyvaluepair <string, Object> item in DIC)
{
Xmlelement element = Doc. createelement (item. Key );
Keyvalue2xml (element, item );
Nroot. appendchild (element );
}
Return Doc;
}

Private Static void keyvalue2xml (xmlelement node, keyvaluepair <string, Object> source)
{
Object kvalue = source. value;
If (kvalue. GetType () = typeof (Dictionary <string, Object> ))
{
Foreach (keyvaluepair <string, Object> item in kvalue as Dictionary <string, Object>)
{
Xmlelement element = node. ownerdocument. createelement (item. Key );
Keyvalue2xml (element, item );
Node. appendchild (element );
}
}
Else if (kvalue. GetType () = typeof (object [])
{
Object [] O = kvalue as object [];
For (INT I = 0; I <O. length; I ++)
{
Xmlelement xitem = node. ownerdocument. createelement ("item ");
Keyvaluepair <string, Object> item = new keyvaluepair <string, Object> ("item", O [I]);
Keyvalue2xml (xitem, item );
Node. appendchild (xitem );
}

}
Else
{
Xmltext text = node. ownerdocument. createtextnode (kvalue. tostring ());
Node. appendchild (text );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.