Testjsonasxmlnodeattribute
usingNewtonsoft.json;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;namespaceparsexml{classProgram {Static voidMain (string[] args) {Testjsonasxmlnodeattribute (); } Public Static voidTestjsonasxmlnodeattribute () {stringPath =@"D:\testSSGeneration\XMLforTestSSGeneration.xml"; stringOutpath =@"D:\testSSGeneration\xmlWithFunAttr.xml"; //1. Read XML and insert a JSON result as a attribute of root nodeXmlDocument Doc=NewXmlDocument (); Doc. Load (path); XmlNode Root=Doc. FirstChild; XmlElement XE= (XmlElement) root;;//To Convert a child node type to a XmlElement typeList<string> list =Newlist<string>(); List. ADD ("param1"); List. ADD ("param2"); Dictionary<string, list<string>> dic =Newdictionary<string, list<string>>(); Dic. ADD ("ff1", list); Dic. ADD ("FF2", list); //Serializing Objects stringJsonstr = Jsonconvert.serializeobject (DIC);//convert objects to JSON storageXe. SetAttribute ("Func", JSONSTR); //2. Serilization to XMLFileDoc. Save (Outpath);//Save//3. Deserilization to XMLFileXmlDocument Testxmldoc =NewXmlDocument (); Testxmldoc.load (Outpath); XmlNode Testroot=Testxmldoc.firstchild; XmlAttributeCollection attrlist=Testroot. Attributes; XmlAttribute fffattr= attrlist["Func"]; Console.WriteLine ("fffattr. Value:"+fffattr. Value); //4. Converting JSON to ObjectsConsole.WriteLine ("fffattr. Value-json"); Dictionary<string, list<string>> Testdic = jsonconvert.deserializeobject<dictionary<string, list<string>>>(fffattr. Value); foreach(keyvaluepair<string, list<string>> KVPinchtestdic) {Console.WriteLine ("Key={0},value={1}", KVP. Key, kvp. value[0]); } }