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
- object 与xml的转换读取
- JAXB XML与Object之间的转换
- Xml与Java Object 的转换[JAXB]
- JAXB:Java Object与XML的互相转换
- 使用XStream, 对于java object与xml之间的转换
- xstream实现xml与Object之间的自如转换
- JAXB实现Object与XML转换
- 20140704-C#object/List<object>与json、xml之间的相互转换
- Java与XML的故事二:XML与Java Object互相转换
- String与object的转换
- XML 和 Java Object 的数据转换
- html的“object HTMLDivElement”与jquery“object Object”相互转换
- xml object 转换
- Map与Object之间的转换
- c# object与byte[] 间的转换
- spring jaxb Object XML转换
- Xstream转换xml到Object的特殊情况
- XML,Object,Json转换之浅析Xstream的使用
- Spring MVC @Transactional注解方式事务失效的解决办法
- WinRT - Disable WebView's scrollbar and allow itself scroll able within a ScrollViewer
- 十大知名厂商ERP解决方案集锦
- Cocos2dx CCSprite CCSpriteFrame CCTexture2D CCAnimation学习总结
- 转载--淘宝2012笔试题
- object 与xml的转换读取
- oc中NSFileHandle的初学-文件读取
- 常见控件(五)
- 地理坐标系、大地坐标系与地图投影与重投影详解
- yum的配置以及 yum.conf 配置文件的说明
- contains a file system with errors, check forced解决方法
- Nokia xl 4G 主内存卡切换
- 第二十四周项目4-猴子选大王(约瑟夫问题)
- 递归算法经典实例