Exemple #1
0
        public Node GetNodeById(string id)
        {
            var nodePath = Path.Combine(_rootPath, id);
            if (!Directory.Exists(nodePath))
                return null;
            var metaDataXmlFile = Path.Combine(nodePath, "metadata.xml");

            var xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.IgnoreWhitespace = true;
            xmlReaderSettings.IgnoreComments = true;

            // read metadata.xml

            var xml2NodesMetadataConverter = new Xml2NodesMetadataConverter();
            var nodeMetadata = xml2NodesMetadataConverter.Extract(metaDataXmlFile);
            dynamic node = new Node(id, nodeMetadata);


            //read data.xml
            var dataXmlFile = Path.Combine(nodePath, "data.xml");
            using (XmlReader xmlReader = XmlReader.Create(dataXmlFile, xmlReaderSettings))
            {
                bool visitNode = false;
                while (xmlReader.Read())
                {
                    if (!xmlReader.Name.Equals("Node") && visitNode)
                    {
                        if (xmlReader.HasAttributes)
                        {
                            var msdocXml = xmlReader["File"];
                            var fs = new FileStream(Path.Combine(nodePath, msdocXml), FileMode.Open, FileAccess.Read);
                            var sr = new StreamReader(fs, Encoding.UTF8);
                            var msdoc = new MsDoc();
                            msdoc.Content = sr.ReadToEnd();
                            node[xmlReader.Name]= msdoc;
                            sr.Close();
                            fs.Close();
                        }
                        else
                        {
                            Type type = NodeMetadataHelper.GetPropertyMetadata(node.Metadata, xmlReader.Name).Type;
                            if (type.Equals(typeof(int)))
                            {
                                node[xmlReader.Name] = Convert.ToInt32(xmlReader.ReadString());
                            }
                            else if (type.Equals(typeof(decimal)))
                            {
                                node[xmlReader.Name]= Convert.ToDecimal(xmlReader.ReadString());
                            }
                            else if (type.Equals(typeof(string)))
                            {
                                node[xmlReader.Name]= xmlReader.ReadString();
                            }
                        }
                    }
                    else if (xmlReader.Name.Equals("Node")) visitNode = true;
                }
            }

            return node;
        }
 public void Test1()
 {
     var xml2Converter = new Xml2NodesMetadataConverter();
     var nodeMetadata = xml2Converter.Extract(CorrectFileName);
     nodeMetadata.Name.Should().Be("题目");
 }