如何搜索xml文件并编辑结果 [英] How to Search through an xml file and edit the results

查看:70
本文介绍了如何搜索xml文件并编辑结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

任何人都可以帮我解决如何搜索xml文件然后编辑结果,例如这里是我的xml文件如下所示:



Hi
Can anyone help me out with how to search through an xml file and then edit the results, for example here is my how my xml file looks like below :

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<GroupPosts>
  <IndividualPosts>
    <PostID>0</PostID>
    <StudentID>106</StudentID>
    <PostTitle>Math Problems</PostTitle>
    <PostDescription>How Do i study</PostDescription>
  </IndividualPosts>

  <IndividualPosts>
    <PostID>1</PostID>
    <StudentID>0</StudentID>
    <PostTitle>I  have passed</PostTitle>
    <PostDescription>i studied harder</PostDescription>
  </IndividualPosts>

<IndividualPosts>
    <PostID>3</PostID>
    <StudentID>3</StudentID>
    <PostTitle>How to solve math problems</PostTitle>
    <PostDescription>i studied harder</PostDescription>
  </IndividualPosts>


</GroupPosts>









我想做的就是这个,使用postId元素搜索文件,检索结果并编辑这些结果并将更改保存到该文件。搜索部分我没有问题。但是,如何编辑搜索结果并将更改保存到该文件。这是我的搜索代码:







What I want to do is this, search through the file using the postId element, retrieve the results and and the edit those results and save the changes to that file. The searching part i have no problem. But how do I edit the search results and save the changes to that file. Here is my code for searching:

public  void getPost(string grpName, int i)
       {
           XmlReader xmlFile;
           xmlFile = XmlReader.Create("fileName.xml", new XmlReaderSettings());
           DataSet ds = new DataSet();
           DataView dv;
           ds.ReadXml(xmlFile);

           dv = new DataView(ds.Tables[0]);
           dv.Sort = "PostID";
           int index = dv.Find("3");



           listBox1.Items.Add(dv[index]["PostID"].ToString());
           listBox1.Items.Add(dv[index]["StudentID"].ToString());
           listBox1.Items.Add(dv[index]["PostDescription"].ToString());
            listBox1.Items.Add(dv[index]["PostTitle"].ToString());


       }





如何编辑结果保存更改相同的文件,我该怎么做,如果有人提供有用的链接,可能会帮助我,我真的很感激。提前谢谢



How do I edit the results save the changes to that same file and how do I go about it, if anyone were to provide me with useful links that might help me I would really appreciate it. Thanks in advance

推荐答案

您希望使用xsl / xslt转换xml,并对除要更改的数据之外的所有数据执行标识转换。为你想要更改的数据添加一个xslt规则,然后(duh)在那里进行更改。



身份转换如下:



You want to transform the xml using xsl/xslt and perform the identity transform on all data except for the data you want to change. Add an xslt rule for the data you want changed and (duh) make the change there.

The identity transform looks like:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|*|processing-instruction()|comment()">
    <xsl:copy>
      <xsl:apply-templates select="@*|*|text()|processing-instruction()|comment()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>





[此转换不会保留的唯一内容是格式化,白色sp ace或CDATA,但这似乎不是你在这里提供的数据的问题。]



[The only things this transform will not preserve are formatting, white space or CDATA but that does not seem to be an issue for the data you present here.]


使用linq很容易在添加引用
it will be easy using linq try this code before add reference
using System.Xml.Linq;




string xmlFileName = @"fileName.xml";
            XDocument XmlDoc = XDocument.Load(xmlFileName);
            List<XElement> XmlElements = (from fx in XmlDoc.Root.DescendantsAndSelf("IndividualPosts") where (((fx.Element("PostID").Value == "3")) select fx).ToList();
            foreach (XElement gg in XmlElements)
            {
                gg.Element("PostID").Value = "addNewValue1";
                gg.Element("StudentID").Value = "addNewValue2";
                gg.Element("PostDescription").Value = "addNewValue3";
                gg.Element("PostTitle").Value = "addNewValue4";
            }
            XmlDoc.Save(xmlFileName);



也知道xml和linq操作请参考以下链接..

http://www.dotnetcurry.com/ShowArticle.aspx?ID=564 [ ^ ]


这篇关于如何搜索xml文件并编辑结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆