object 与xml的转换读取

来源:互联网 发布:matlab矩阵范数 编辑:程序博客网 时间:2024/05/22 00:24
</pre><pre name="code" class="csharp"><pre name="code" class="csharp">
1.object ====> xml XmlDocument doc = new XmlDocument(); XmlElement Root = doc.CreateElement("Table"); //主内容 doc.AppendChild(Root);List<string> eleColName=new List<string>();List<string> eleValue=new List<string>();//生成xml for (int k = 0; k < eleColName.Count; k++){         XmlElement Child = doc.CreateElement(eleColName[i]);          Child.InnerText = eleValue[k];           Root.AppendChild(Child); }if (doc.SerializeXml().IsNotNullAndEmpty()) {       string Xml = doc.SerializeXml().Substring(doc.SerializeXml().IndexOf('>') + 3);}2.xml =====>object  System.Xml.XmlDocument doc = new System.Xml.XmlDocument();   string xml = bill != null ? bill.Xml : "";   doc.LoadXml(xml);   XmlNodeList xnl = doc.SelectSingleNode("Table").ChildNodes;   foreach (XmlNode xn in xnl)   {       string name = xn.Name       string value =  xn.InnerText                       }

3.实例更新xml(新增子节点或删除修改)

<pre name="code" class="csharp"> xml:
<Template>
<Category text="备件库存导入列" key="KCNeedCKColumnNames">    <Column>      <Id>001</Id>      <Key>sku</Key>      <Value>        <Name>物料编码</Name>      </Value>    </Column><pre name="code" class="csharp"></Category></Template>


</pre></div><pre>
/// <summary>/// 更新xml(新增子节点或删除修改)  </summary>
/// <param name="key">类型</param>/// <param name="id">修改列的编号</param>
/// <param name="names">别名</param>
       private void EditColoum(string key, string id, string names)        {            string stype = "0";            try            {                XmlDocument doc = new XmlDocument();                doc.Load(Function.GetMapPath("/xml/ImportTemplate.xml"));                XmlNode root = doc.SelectSingleNode("Template"); //查找                if (root == null) return;                XmlNodeList nodeCas = root.SelectNodes("Category"); //查找                if (nodeCas == null) return;                foreach (XmlNode nodeCa in nodeCas)                {                    if (nodeCa.Attributes==null) continue;                    XmlAttribute att = nodeCa.Attributes["key"];                    if (att == null) continue;                    if (att.Value == key)                    {                        XmlNodeList nodeItems = nodeCa.SelectNodes("Column"); //查找                        if (nodeItems == null) return;                        foreach (XmlNode nodeItem in nodeItems)                        {                            XmlNodeList attId = nodeItem.SelectNodes("Id");                            XmlNodeList attKey = nodeItem.SelectNodes("Key");                            XmlNodeList attValues = nodeItem.SelectNodes("Value");                            XmlNode attValue = nodeItem.SelectSingleNode("Value");                            if (attId != null && attId.Count > 0 && attKey != null && attKey.Count > 0)                            {                                if (id == attId[0].InnerText)                                {                                    if (attValues != null)                                    {                                        foreach (XmlNode xn in attValues)                                        {                                            XmlElement xe = (XmlElement)xn;                                            xe.RemoveAll();                                        }                                    }                                    if (attValue == null)                                    {                                        attValue = doc.CreateElement("Value");                                        nodeItem.AppendChild(attValue);                                    }                                    foreach (var name in names.Split(","))                                    {                                        XmlElement ele = GetXmlElement(doc, "Name", name);                                        attValue.AppendChild(ele);                                    }                                    break;                                }                            }                        }                    }                }                doc.Save(Function.GetMapPath("/xml/ImportTemplate.xml"));            }            catch (Exception ex)            {            }        }


0 0