专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > LINQ

求教linq to xml 的功能解决方案

发布时间:2011-06-24 21:55:19 文章来源:www.iduyao.cn 采编人员:星星草
求教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");
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: