</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 }
<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) { } }
原文地址:https://www.jb51.cc/xml/297130.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。