日期:2014-05-20  浏览次数:20804 次

求教linq to xml 的功能
C# code

<DataBind>
  <ListItem>
    <ListItemID>1</ListItemID>
    <DataBindCateID>1</DataBindCateID>
    <DataValueField>1</DataValueField>
    <DataTextField>订单问题</DataTextField>
    <DataColorField>#F70909</DataColorField>
    <ParentID>0</ParentID>
  </ListItem>
  <ListItem>
    <ListItemID>2</ListItemID>
    <DataBindCateID>1</DataBindCateID>
    <DataValueField>1</DataValueField>
    <DataTextField>订单取消问题</DataTextField>
    <DataColorField>#000000</DataColorField>
    <ParentID>1</ParentID>
  </ListItem>
  <ListItem>
    <ListItemID>3</ListItemID>
    <DataBindCateID>1</DataBindCateID>
    <DataValueField>Dear customer,

We have temporarily held your order. 

Would you please share us why you want to cancel your order? 

If there’s problem with the SKU, quantity or the shipping address,we can update it for you.



Sincerely yours’
{0}</DataValueField>
    <DataTextField>询问订单取消原因</DataTextField>
    <DataColorField>#000000</DataColorField>
    <ParentID>2</ParentID>
  </ListItem>
</DataBind>
以上为XML

我现在就想根据指定的 ListItemID =我要的参数来修改里面的节点值,比如我传个ID为2的,则修改 =2下面的 
DataTextField, DataColorField,DataValueField 相关属性的值,



求高手帮忙解决呀

------解决方案--------------------
C# code

void Main()
{
    string xml=@"<DataBind>
  <ListItem>
    <ListItemID>1</ListItemID>
    <DataBindCateID>1</DataBindCateID>
    <DataValueField>1</DataValueField>
    <DataTextField>订单问题</DataTextField>
    <DataColorField>#F70909</DataColorField>
    <ParentID>0</ParentID>
  </ListItem>
  <ListItem>
    <ListItemID>2</ListItemID>
    <DataBindCateID>1</DataBindCateID>
    <DataValueField>1</DataValueField>
    <DataTextField>订单取消问题</DataTextField>
    <DataColorField>#000000</DataColorField>
    <ParentID>1</ParentID>
  </ListItem>
  <ListItem>
    <ListItemID>3</ListItemID>
    <DataBindCateID>1</DataBindCateID>
    <DataValueField>Dear customer,

We have temporarily held your order. 

Would you please share us why you want to cancel your order? 

If there’s problem with the SKU, quantity or the shipping address,we can update it for you.



Sincerely yours’
{0}</DataValueField>
    <DataTextField>询问订单取消原因</DataTextField>
    <DataColorField>#000000</DataColorField>
    <ParentID>2</ParentID>
  </ListItem>
</DataBind>";
 
   XElement data=XElement.Parse(xml);
   var query=(from x in data.Descendants("ListItem")
              where x.Element("ListItemID").Value=="2"
              select x).FirstOrDefault();
    if(query !=null)
    {
      query.SetElementValue("DataColorField","new color");
      query.SetElementValue("DataTextField","new text");
      query.SetElementValue("DataValueField","new value");
     
      data.Save("c:\\test.xml");
       
    }
    
     
}

------解决方案--------------------
mark
------解决方案--------------------
C# code

XElement elem = XElement.Load(@"G:\DataSet.xml");
               XElement ssv = (from u in elem.Elements("Table")
                               //你的条件
                               where u.Element("Age").Value == 45.ToString()
                               select u).FirstOrDefault();

               Console.WriteLine();
               if (ssv != null)
               {
                   //这里是要修改的属性
                   //自己一条条的改吧
                   ssv.Element("Name").Value = "mmmm";
               }
               elem.Save(@"G:\DataSet.xml");