加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

object 与xml的转换读取

发布时间:2020-12-16 09:15:45 所属栏目:百科 来源:网络整理
导读:/prepre name="code" class="csharp"pre name="code" class="csharp" 1.object ==== xml XmlDocument doc = new XmlDocument(); XmlElement Root = doc.CreateElement("Table"); //主内容 doc.AppendChild(Root);Liststring eleColName=new Liststring();Lis
</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)
            {
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读