예제 #1
0
        private string ToJsonMulti()
        {
            string result = "";

            List <XmlNode> molecules = GetMolecules();

            JObject   jsonOut            = new JObject();
            JArray    jMoleculesArray    = new JArray();
            JProperty jMoleculesProperty = new JProperty("m", jMoleculesArray);

            jsonOut.Add(jMoleculesProperty);

            foreach (XmlNode molecule in molecules)
            {
                CmlHelper helper = new CmlHelper();
                string    molXml = molecule.OuterXml;
                helper.LoadFromMoleculeCml(molXml);
                JObject m = JObject.Parse(helper.ToJsonSingle());
                jMoleculesArray.Add(m);
            }

            result = jsonOut.ToString();

            return(result);
        }
예제 #2
0
        private void ParseJsonMulti(string input)
        {
            List <CmlHelper> molecules = new List <CmlHelper>();

            JToken molJson = JObject.Parse(input);

            JToken mols       = molJson.SelectToken("m");
            int    atomNumber = 0;
            int    bondNumber = 0;

            foreach (JToken mol in mols)
            {
                CmlHelper molHelper = new CmlHelper(mol.ToString(), atomNumber, bondNumber);
                atomNumber = molHelper.AtomCounter;
                bondNumber = molHelper.BondCounter;
                molecules.Add(molHelper);
            }

            _xmlDocument         = CreateCmlDocument(molecules[0].GetMolecule().OuterXml, true);
            _xmlNamespaceManager = new XmlNamespaceManager(_xmlDocument.NameTable);
            _xmlNamespaceManager.AddNamespace(NamespacePrefix, NamespaceUri);

            int moleculeCounter = 0;

            SetMoleculeId("m" + moleculeCounter);
            moleculeCounter++;

            XmlNode cmlNode = _xmlDocument.SelectSingleNode("//cml:cml", _xmlNamespaceManager);

            for (int i = 1; i < molecules.Count; i++)
            {
                molecules[i].SetMoleculeId("m" + moleculeCounter);
                moleculeCounter++;

                XmlNode             mol      = molecules[i].GetMolecule();
                XmlDocumentFragment fragment = _xmlDocument.CreateDocumentFragment();
                fragment.InnerXml = mol.OuterXml;
                cmlNode.AppendChild(fragment);
            }
        }