需求如下:
<?xml version="1.0">
  <ListSection>
  <ListPart>
  <ListNo>1</ListNo>
  <ListSymbol>test1</ListSymbol>
  <ListComment>test1Comment</ListComment>
  </ListPart>
  <ListPart>
  <ListNo>2</ListNo>
  <ListSymbol>test2</ListSymbol>
  <ListComment>test2Comment</ListComment>
  </ListPart></ListSection>xXDocument xmlSource = XDocument.Load(new StringReader(strXml));
想通过xmlSource 给ListSymbol=test2的节点追加一个属性并附上值,即<ListSymbol attr="hello">test2</ListSymbol>
如何做呢,请教大家帮帮忙。谢谢

解决方案 »

  1.   

    xmlSource.Elements().Skip(1).First().Elements("ListSymbol").First().Add(new XAttribute("attr", "hello")); 
      

  2.   

    // 设定根节点ItemsCount属性值
        // Parameter:
        //  RootNodeName:根节点名称
        //  ds:数据集
        // Return:
        //      XmlDataDocument
        private XmlDataDocument SetItemsCountAttribute(string strRootNodeName, DataSet ds)
        {
            try
            {
                XmlDataDocument xmlDoc;
                int ItemCount = 0;            ds.DataSetName = strRootNodeName;
                ds.EnforceConstraints = false;
                if (ds.Tables.Count == 0  )
                {
                    xmlDoc = new XmlDataDocument();                string xml = "<" + strRootNodeName + "></" + strRootNodeName + ">";
                    xmlDoc.LoadXml(xml);
                }
                else
                {
                    ds.Tables[0].TableName = "Item";                ItemCount = ds.Tables[0].Rows.Count;
                    if (ItemCount == 0)
                    {
                        xmlDoc = new XmlDataDocument();                    string xml = "<" + strRootNodeName + "></" + strRootNodeName + ">";
                        xmlDoc.LoadXml(xml);
                    }
                    else
                    {
                        xmlDoc = new XmlDataDocument(ds);
                    }
                }            XmlNode root = xmlDoc.FirstChild;
                // 创建节点
                XmlNode attrCount = xmlDoc.CreateNode(XmlNodeType.Attribute, "ItemsCount", null);
                attrCount.Value = ItemCount.ToString();
                // 添加节点属性
                root.Attributes.SetNamedItem(attrCount);            return xmlDoc;
            }
            catch (Exception e)
            {
                string strMsg = e.Message;
                return null;
            }
        }
      

  3.   

    代码已经经过测试:
    var document = XDocument.Load("XMLFile1.xml");            var query =  from ele in document.Descendants(XName.Get("ListSymbol"))
                             where ele.Value == "test2"
                             select ele;
                var node = query.SingleOrDefault();
                if (node != null)
                {
                    node.Add(new XAttribute("attr", "hello"));
                }            document.Save("XmlResult.xml");
    XmlResult.xml:<?xml version="1.0" encoding="utf-8"?>
    <ListSection>
      <ListPart>
        <ListNo>1</ListNo>
        <ListSymbol>test1</ListSymbol>
        <ListComment>test1Comment</ListComment>
      </ListPart>
      <ListPart>
        <ListNo>2</ListNo>
        <ListSymbol attr="hello">test2</ListSymbol>
        <ListComment>test2Comment</ListComment>
      </ListPart>
    </ListSection>
      

  4.   

    +1
    用linq to xml 爽.