XML简单操作-XSL

来源:互联网 发布:治风湿病专科医院知乎 编辑:程序博客网 时间:2024/04/28 01:23
 

1,XML中特殊符号输入;
&lt; < 小于
&gt; > 大于
&amp; & 和号
&apos; ' 单引号
&quot; " 引号
2,读取XML文件(加载XML)
 XmlDocument doc = new XmlDocument();
            doc.Load(File.OpenRead(Server.MapPath("textread1.xml")));
3,保存XML
doc.saveas(Server.MapPath("doc.xml"));
doc.Save("doc.xml");

4、获取节点值
doc.SelectSingleNode("Pnet/Data/Domatter").ChildNodes[0].Attributes[0].Value;
doc.SelectSingleNode("Pnet/Data/Domatter").Attributes.GetNamedItem("Value1").Value;
            }
  doc.selectSingleNode("Pnet/Data/Domatter").attributes.getNamedItem("name").value;
5、添加节点及设置节点值
XmlNode node = doc.selectSingleNode("Node");
XmlElement newnode = doc.createElement("newNode");
node.appendChild(newnode);

newnode.setAttribute("Name",(string)value);
6、修改节点值
XmlNode node = doc.selectSingleNode("Data");
XmlElement xe = (XmlElement)node;
if(xe.getAttribute("Name")==value)
xe.setAttribute("Value1",(string)newvalue);

if(xe.Name=="value")
xe.innerText=newvalue;

7、删除子节点
xe.RemoveAttribute("Name");  删除名为Name的属性
xe.RemoveAll();  删除节点内全部内容

8、遍历子节点
foreach(XmlNode xn in doc){
XmlElement xe = (XmlElement)xn;

}
XmlNodeList xnl = xe.ChildNodes;
foreach(XmlNode xnn in xnl){
XmlElement xee = (XmlElement)xnn;

}

可以将xml看作是网页,将xsl看作是css样式表,不过要精确到每一个元素

 p3.xml内容

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="p3.xsl"?>
<books>
    <book>
        <name>The C++ Standard Library</name>
        <author>Nicolai M.Josuttis</author>
    </book>
    <book>
        <name>The Mythical Man-Month</name>
        <author>Frederick P Brooks Jr.</author>
    </book>
    <book>
        <name>C# Design Pattern</name>
        <author>James W. Cooper</author>
    </book>
</books>


p3.xsl内容

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    <xsl:template match="/">
        <html>
            <head><title>Book Store</title></head>
            <body>
                <xsl:apply-templates select="books"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="books">
        <table border="1" cellpadding="0" align="center">
            <tr bgcolor="red"><th>Name</th><th>Author</th></tr>
            <xsl:for-each select="book">
                <tr>
                    <td><xsl:value-of  select="name"/></td>
                    <td><xsl:value-of  select="author"/></td>
                </tr>
            </xsl:for-each>
        </table>
</xsl:template>
</xsl:stylesheet>

 效果如图片1.jpg

 

 

 

 

 

 

原创粉丝点击