首页 新闻 会员 周边

合并XML相同节点

0
[已解决问题] 解决于 2014-07-18 14:45

如下的xml中:

<tag>        

<id>3428083006598920604</id>        

<name>上网</name>      

</tag>           

<tag>        

<id>7632343735443733612</id>       

  <name>听音乐</name>      

</tag>  

我想把以上的xml变成一下格式:

<tag> 上网,听音乐 </tag>

请问C#怎么来解析呢?

悠扬的牧笛的主页 悠扬的牧笛 | 初学一级 | 园豆:156
提问于:2014-01-04 11:50
< >
分享
最佳答案
0

奖励园豆:5
aehyok | 小虾三级 |园豆:1212 | 2014-01-04 13:18

就看你需要如何合并了。

aehyok | 园豆:1212 (小虾三级) | 2014-01-04 13:18
其他回答(1)
0
//以前收集的代码,平时用linq to xml
//第一种方式 序列化
[XmlRoot("products")]
    public class Products
    {
        [XmlElement("product")]
        public Product[] Items { get; set; }
    }
    public class Product
    {
        [XmlAttribute(AttributeName = "name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName = "price")]
        public decimal Price { get; set; }
        [XmlAttribute(AttributeName = "supplier")]
        public decimal Supplier { get; set; }
    }

using (TextReader reader = new StreamReader("1.xml"))
            {
                var serializer = new XmlSerializer(typeof(Products));
                var items = (Products)serializer.Deserialize(reader);
                if (items != null)
                {
                    // items.Items 
                }
                else
                {
                }
            }

//第二种方式 linq to xml
XDocument xdoc = XDocument.Load("1.xml");
            XElement root = xdoc.Root; //products 
            IEnumerable<XElement> ps = root.Elements(); //product(集合)下的所有元素
            foreach (var item in ps)
            {
                IEnumerable<XAttribute> attrs = item.Attributes(); //每个product下每个属性(集合)
                //foreach (var attr in attrs)
                //{
                //    //stringBuilder.AppendFormat("{0}:{1}\n", attr.Name, attr.Value);
                //}
            }
秋壶冰月 | 园豆:5903 (大侠五级) | 2014-01-04 12:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册