Exemplo n.º 1
0
        Dictionary <string, TableGuider> getTableGuiders(XmlNode xmlSchema)
        {
            Dictionary <string, TableGuider> ret = new Dictionary <string, TableGuider>();

            if (xmlSchema == null)
            {
                return(ret);
            }
            XmlNodeList nodes = xmlSchema.SelectNodes("Table");

            foreach (XmlNode node in nodes)
            {
                TableGuider tg = new TableGuider(node);
                if (!ret.ContainsKey(tg.TableName))
                {
                    ret.Add(tg.TableName, tg);
                }
            }
            return(ret);
        }
Exemplo n.º 2
0
        void AddSubTables(XmlNode rootnode, XmlNode cell, string subname, TableGuider tg, XmlNode mainnode, string mainname, XmlNamespaceManager xmlm)
        {
            XmlDocument xmldoc = ((rootnode is XmlDocument) ? rootnode as XmlDocument : rootnode.OwnerDocument);
            //XmlNode subnode = xmldoc.CreateElement(cell.Name, xmlm.LookupNamespace("json"));
            XmlNode subnode = xmldoc.CreateElement(cell.Name);

            //XmlAttribute att = xmldoc.CreateAttribute("json:Array", xmlm.LookupNamespace("json"));
            //att.Value = "true";
            //subnode.Attributes.Append(att);
            rootnode.AppendChild(subnode);
            foreach (XmlNode scell in cell.ChildNodes)
            {
                if (scell.ChildNodes.Count > 1)
                {
                    continue;
                }
                else
                {
                    if (scell.FirstChild is XmlElement)
                    {
                    }
                    else
                    {
                        scell.InnerText = scell.InnerText.Trim();//去除空格
                        subnode.AppendChild(xmldoc.ImportNode(scell, true));
                    }
                }
            }
            string[] keys = tg.Key.Split(tg.KeySplitor.ToCharArray());
            for (int i = 0; i < keys.Length; i++)
            {
                XmlNode keynode      = xmldoc.CreateElement(string.Format("{0}_{1}", mainname, keys[i]));//防止子表有同样的节点,节点以主表+主表key命名
                XmlNode existKeyNode = mainnode.SelectSingleNode(keys[i]);
                if (existKeyNode != null)
                {
                    keynode.InnerText = existKeyNode.InnerText.Trim();
                }
                subnode.AppendChild(keynode);
            }
        }
Exemplo n.º 3
0
        XmlNode FillXmlByDatable(XmlNode parent, DataSet ds, int dtindex, List <TableGuider> guds)
        {
            if (ds.Tables.Count <= dtindex)
            {
                throw new Exception("JdUion:数据集超出索引!");
            }
            if (guds.Count <= dtindex)
            {
                throw new Exception("JdUion:Schema配置超出索引!");
            }

            TableGuider tg    = guds[dtindex];
            TableGuider pretg = null;

            if (dtindex > 0)
            {
                pretg = guds[dtindex - 1];
            }
            if (tg.TableName == null || tg.TableName.Trim().Length == 0)
            {
                throw new Exception("JdUion:Schema配置Json超出索引!");
            }
            DataTable dt = ds.Tables[dtindex];

            string      RepeatItem = guds[dtindex].TableName;
            XmlDocument doc        = parent.OwnerDocument;

            if (doc == null)
            {
                doc = parent as XmlDocument;
            }
            XmlNamespaceManager xmlm = new XmlNamespaceManager(doc.NameTable);

            xmlm.AddNamespace("json", "http://james.newtonking.com/projects/json");//添加命名空间
            string filter = "";

            if (pretg == null || pretg.KeyValue == null)
            {
                filter = "1=1";
            }
            else
            {
                filter = string.Format("{0}='{1}'", pretg.NextRef, pretg.KeyValue);
            }
            DataRow[] drs = dt.Select(filter);
            for (int i = 0; i < drs.Length; i++)
            {
                XmlNode      node = doc.CreateElement(RepeatItem, xmlm.LookupNamespace("json"));
                XmlAttribute att  = doc.CreateAttribute("json:Array", xmlm.LookupNamespace("json"));
                att.Value = "true";
                node.Attributes.Append(att);
                //XmlUtil.AddAttribute(node, "json:Array", "true");
                string keyval = null;
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string  col     = dt.Columns[j].ColumnName;
                    string  val     = drs[i][col].ToString();
                    XmlNode subnode = doc.CreateElement(col);
                    subnode.InnerText = val;
                    if (tg.Key == null || tg.Key.Trim().Length < dtindex + 1)
                    {
                        node.AppendChild(subnode);
                        continue;
                    }
                    if (tg.Key != null && tg.Key == col.ToLower())
                    {
                        keyval = val;
                    }
                    node.AppendChild(subnode);
                }
                if (keyval == null)//没遇到主键,下一行
                {
                    parent.AppendChild(node);
                    continue;
                }
                int NextIdx = dtindex + 1;
                if (ds.Tables.Count <= NextIdx)
                {
                    parent.AppendChild(node);
                    continue;
                }
                guds[NextIdx].KeyValue = keyval;
                node = FillXmlByDatable(node, ds, NextIdx, guds);
                parent.AppendChild(node);
            }
            return(parent);
        }
Exemplo n.º 4
0
        bool getXmlDataSet(XmlDocument xmldoc, XmlSchemaClass xmlSchema, ref XmlDocument retDoc, ref string msg)
        {
            if (xmlSchema == null)
            {
                retDoc = xmldoc;
            }
            retDoc = new XmlDocument();
            Dictionary <string, TableGuider> tablist = xmlSchema.TableList;

            retDoc.LoadXml("<NewDataSet/>");
            if (xmldoc == null)
            {
                return(false);
            }
            try
            {
                if (tablist.Count == 0)
                {
                    msg = "未指定指引配置!";
                    return(false);
                }
                TableGuider tg = tablist.First().Value;
                if (tg == null)
                {
                    msg = "未配置表结构!";
                    return(false);
                }
                List <TableGuider> subtabs  = tablist.Values.Skip(1).ToList();
                string             mainname = tg.TableName;
                XmlNode            rootnode = retDoc.SelectSingleNode("NewDataSet");
                if (string.IsNullOrEmpty(mainname))
                {
                    msg = "指定指引中主表名未配置!";
                    return(false);
                }
                XmlNamespaceManager xmlm = new XmlNamespaceManager(xmldoc.NameTable);
                xmlm.AddNamespace("json", "http://james.newtonking.com/projects/json");//添加命名空间
                XmlNode     root      = xmldoc.SelectSingleNode(tg.rootNodePath);
                XmlNodeList itemnodes = root.SelectNodes(tg.dataItemName);
                string      message   = XmlUtil.GetSubNodeText(root, tg.msgItemName);
                if (!string.IsNullOrEmpty(message))
                {
                    if (message.Trim() != "success" && message.Trim() != "接口成功")
                    {
                        msg = message;
                        return(false);
                    }
                }
                foreach (XmlNode dataNode in itemnodes)
                {
                    string submsg = null;
                    if (XmlUtil.GetSubNodeText(itemnodes[0], tg.msgItemName) != "" && XmlUtil.GetSubNodeText(dataNode, tg.msgItemName) != "0")
                    {
                        submsg = XmlUtil.GetSubNodeText(dataNode, tg.msgItemName);
                        msg    = submsg;
                        return(false);
                    }

                    //XmlNode NewRow = xmldoc.CreateElement(mainname, xmlm.LookupNamespace("json"));
                    XmlNode NewRow = retDoc.CreateElement(mainname);
                    //XmlAttribute att = xmldoc.CreateAttribute("json:Array", xmlm.LookupNamespace("json"));
                    //att.Value = "true";
                    //NewRow.Attributes.Append(att);
                    string tableKey = tg.Key;
                    string keyValue = null;
                    if (tg.Columns.Count > 0)
                    {
                        foreach (string key in tg.Columns.Keys)
                        {
                            ResponseDataColumn rdc      = tg.Columns[key];
                            XmlNode            cellNode = retDoc.CreateElement(key);
                            if (!rdc.outXml)
                            {
                                cellNode.InnerText = XmlUtil.GetSubNodeText(dataNode, rdc.xPath);
                            }
                            else
                            {
                                //System.Web.HttpUtility.UrlEncode
                                string strOutXml = HttpUtility.HtmlEncode(dataNode.SelectSingleNode(rdc.xPath)?.OuterXml);
                                cellNode.InnerXml = strOutXml;
                            }
                            if (key == tableKey)
                            {
                                keyValue = cellNode.InnerText;
                            }
                            NewRow.AppendChild(cellNode);
                        }
                    }

                    rootnode.AppendChild(NewRow);
                    for (int i = 0; i < subtabs.Count; i++)
                    {
                        TableGuider stg        = subtabs[i];
                        string      sItemPath  = string.Format("{0}/{1}", stg.rootNodePath, stg.dataItemName);
                        XmlNodeList sDataNodes = dataNode.SelectNodes(sItemPath);
                        if (sDataNodes.Count == 0)
                        {
                            continue;
                        }
                        XmlNode subTableRow = retDoc.CreateElement(stg.TableName);
                        foreach (XmlNode sitem in sDataNodes)
                        {
                            if (stg.Columns.Count > 0)
                            {
                                bool ExistKey     = false;
                                bool ExistKeyItem = false;
                                foreach (string key in stg.Columns.Keys)
                                {
                                    ResponseDataColumn rdc      = stg.Columns[key];
                                    XmlNode            cellNode = retDoc.CreateElement(key);
                                    if (!rdc.outXml)
                                    {
                                        cellNode.InnerText = XmlUtil.GetSubNodeText(dataNode, rdc.xPath);
                                    }
                                    else
                                    {
                                        cellNode.InnerXml = XmlUtil.GetSubNodeXml(dataNode, rdc.xPath);
                                    }
                                    subTableRow.AppendChild(cellNode);
                                    if (key == tableKey)
                                    {
                                        ExistKeyItem = true;
                                        if (cellNode.InnerText == keyValue)
                                        {
                                            ExistKey = true;
                                        }
                                    }
                                }
                                XmlNode keyNode = null;
                                if (ExistKeyItem)  //存在键项
                                {
                                    if (!ExistKey) //存在同名键,另外新建一个表名_健名,如果虚拟键恰巧存在就通过修改表名避免
                                    {
                                        string  parentKey      = string.Format("{0}_{1}", tg.TableName, tableKey);
                                        XmlNode virtualKeyNode = retDoc.CreateElement(parentKey);
                                        virtualKeyNode.InnerText = keyValue;
                                        subTableRow.AppendChild(virtualKeyNode);
                                    }
                                }
                                else //没有存在键相,直接插入一个
                                {
                                    keyNode           = retDoc.CreateElement(tableKey);
                                    keyNode.InnerText = keyValue;
                                    subTableRow.AppendChild(keyNode);
                                }
                            }
                        }
                        rootnode.AppendChild(subTableRow);
                    }
                }
                return(true);
            }
            catch (Exception ce)
            {
                msg = string.Format("{0}[{1}]", ce.Message, ce.StackTrace);
                return(false);
            }
        }