日期:2014-05-18  浏览次数:20470 次

求解 asp.net 对xml的操作
<?xml version="1.0" encoding="utf-8" ?>
<Table>
  <Row >
  <Title>首页</Title> 
  <Name>企业首页</Name> 
  <Href>_self</Href> 
  <Url>index.html</Url> 
  <Mark>index</Mark> 
  </Row>
  <Row >
  <Title>产品展示</Title> 
  <Name>产品展示</Name> 
  <Href>_self</Href> 
  <Url>Pro.html</Url> 
  <Mark>Pro</Mark> 
  </Row>
  </Table>

怎么通过 <Mark>的值返回父节点中的所有节点。也就是说,当我接收<Mark>值为index时,把 
  <Title>首页</Title> 
  <Name>企业首页</Name> 
  <Href>_self</Href> 
  <Url>index.html</Url> 
  <Mark>index</Mark> 
以上内容返回到Dataset中

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

        string strXml = @"<Table>
                              <Row >
                              <Title>首页</Title>  
                              <Name>企业首页</Name>  
                              <Href>_self</Href>  
                              <Url>index.html</Url>  
                              <Mark>index</Mark>  
                              </Row>
                              <Row >
                              <Title>产品展示</Title>  
                              <Name>产品展示</Name>  
                              <Href>_self</Href>  
                              <Url>Pro.html</Url>  
                              <Mark>Pro</Mark>  
                              </Row>
                              </Table>";

        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(strXml);
        string strXPath = "/Table/Row[Mark='index']";
        XmlNodeList oXmlNodes = xdoc.SelectNodes(strXPath);
        //*************已经成取到所需的信息了***********


        xdoc = new XmlDocument();
        XmlDeclaration xmlDec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
        XmlElement rootNode = xdoc.CreateElement("Table");
        xdoc.InsertBefore(xmlDec, xdoc.DocumentElement);
        xdoc.AppendChild(rootNode);
        foreach (XmlNode node in oXmlNodes)
        {
            XmlElement xmlNew = xdoc.CreateElement("Row");
            xmlNew.InnerXml = node.InnerXml;
            rootNode.AppendChild(xmlNew);
        }

        DataSet ds = new DataSet();
        StringReader stream = new StringReader(xdoc.InnerXml);
        XmlTextReader reader = new XmlTextReader(stream);
        ds.ReadXml(reader);