标签:
<?xml version="1.0" encoding="utf-8"?>
<根节点>
<字符串>测试字符串!!!ABCD</字符串>
<数字 属性1="李志伟" 属性2="lizhiwei">123.456</数字>
</根节点>
class Program
{
static void Main(string[] args)
{
XmlReader rxml = XmlReader.Create(@"..\..\XML\XML.xml");
while (rxml.Read())//读取节点
{ //操作当前节点
Console.WriteLine("节点名:" + rxml.Name + "\t节点类型:" + rxml.NodeType);
for (int i = 0; i < rxml.AttributeCount; i++)
{
Console.WriteLine("属性:"+rxml.GetAttribute(i));//读取属性
}
if (rxml.Name == "数字")
{ //读取数字
Console.WriteLine("数字值:" + rxml.ReadElementContentAsDouble()*100);
}
else if (rxml.Name == "字符串")
{//读取字符串
Console.WriteLine("字符串值:" + rxml.ReadElementContentAsString());
}
Console.WriteLine("====================================================");
}
rxml.Close();
Console.Read();
}
}
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="根节点">
<xs:complexType>
<xs:sequence>
<xs:element name="字符串" type="xs:string"/>
<xs:element name="数字">
<xs:complexType>
<xs:attribute name="属性1" type="xs:string"/>
<!-- 未定义属性2-->
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
class Program
{
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, @"..\..\XML\XMLSchema.xsd");
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(Error);
XmlReader rxml = XmlReader.Create(@"..\..\XML\XML.xml", settings);
while (rxml.Read())//读取节点
{//操作当前节点
Console.WriteLine("节点名:" + rxml.Name + "\t节点类型:" + rxml.NodeType);
for (int i = 0; i < rxml.AttributeCount; i++)
{
Console.WriteLine("属性:"+rxml.GetAttribute(i));//读取属性
}
if (rxml.Name == "数字")
{//读取数字
Console.WriteLine("数字值:" + rxml.ReadElementContentAsDouble()*100);
}
else if (rxml.Name == "字符串")
{//读取字符串
Console.WriteLine("字符串值:" + rxml.ReadElementContentAsString());
}
Console.WriteLine("====================================================");
}
rxml.Close();
Console.Read();
}
//读取XML错误时调用的方法
static void Error(object sender, ValidationEventArgs e)
{
Console.WriteLine("########XML格式出错:"+e.Message);
}
}
class Program
{
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;//设置使用缩进
settings.NewLineOnAttributes = true;//设置将属性写入新行
XmlWriter wxml = XmlWriter.Create(@"..\..\XML\newXML.xml",settings);
wxml.WriteStartDocument();//写入XML的申明
wxml.WriteStartElement("根节点");//开始写根节点==
wxml.WriteStartElement("节点");//开始写节点
wxml.WriteAttributeString("姓名", "李志伟");//写属性
wxml.WriteAttributeString("年龄", "20");//写属性
wxml.WriteString("李志伟lizhiwei");//写节点内容
wxml.WriteEndElement();//节点写完
wxml.WriteElementString("标题","测试节点!!!ABCD");//直接写无属性节点
wxml.WriteEndElement();//根节点写完==
wxml.Close();
Console.WriteLine("OK!");
Console.Read();
}
}
<?xml version="1.0" encoding="utf-8"?>
<根节点>
<节点>元素类容</节点>
<数字 属性1="李志伟" 属性2="lizhiwei">123.456</数字>
<数字>123456.789</数字>
<子节点>
<整数>123456</整数>
<字符串>李志伟</字符串>
<空节点 作用="测试插入节点">
</空节点>
</子节点>
</根节点>
class Program
{
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"..\..\XML\XML.xml");//加载XML文件
//得到一组节点
XmlNodeList nodes = xml.GetElementsByTagName("数字");
foreach (XmlNode node in nodes)
{
Console.WriteLine("节点=======" + node.Name + "\t" + node.InnerText);
foreach (XmlAttribute a in node.Attributes)
{
Console.WriteLine("属性:" + a.Name + "\t" + a.Value);
}
}
//创建要插入的节点
XmlElement element = xml.CreateElement("插入节点");//创建节点
element.SetAttribute("姓名", "李志伟");//设置属性
element.SetAttribute("年龄", "20");//设置属性
XmlElement child = xml.CreateElement("插入子节点");//创建节点
child.InnerText = "测试插入节点!ABCD";//设置节点内容
element.AppendChild(child);
//设置插入节点的位置
nodes = xml.GetElementsByTagName("空节点");
nodes[0].AppendChild(element);//插入节点
xml.Save(@"..\..\XML\XML.xml");
Console.WriteLine("OK!");
Console.Read();
}
}
<?xml version="1.0" encoding="utf-8"?>
<根节点>
<节点 节点名="A">
<子节点1>AAA子节点1</子节点1>
<子节点2>AAA子节点2</子节点2>
<子节点3>AAA子节点3</子节点3>
</节点>
<节点 节点名="B">
<子节点1>BBB子节点1</子节点1>
<子节点2>BBB子节点2</子节点2>
<子节点3>BBB子节点3</子节点3>
</节点>
<节点 节点名="A">
<子节点1>CCC子节点1</子节点1>
<子节点2>CCC子节点2</子节点2>
<子节点3>CCC子节点3</子节点3>
</节点>
</根节点>
class Program
{
static void Main(string[] args)
{
XPathDocument xml = new XPathDocument(@"..\..\XML\XML.xml");
XPathNavigator nav = xml.CreateNavigator();
XPathNodeIterator iter = nav.Select("根节点/节点[@节点名=‘A‘]");//查询
while (iter.MoveNext())//下一个结果
{
Console.WriteLine(iter.Current.Name + "\t" + iter.Current.Value);
XPathNodeIterator temp = iter.Current.SelectDescendants(XPathNodeType.Element,false);//查询
while (temp.MoveNext())//下一个结果
{
Console.WriteLine(temp.Current.Name + "\t" + temp.Current.Value);
}
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"..\..\XML\XML.xml");
XPathNavigator nav = xml.CreateNavigator();
XPathNodeIterator iter = nav.Select("根节点/节点[@节点名=‘A‘]");//查询
while (iter.MoveNext())
{
Console.WriteLine(iter.Current.Name + "\t" + iter.Current.Value);
XPathNodeIterator temp = iter.Current.SelectDescendants(XPathNodeType.Element,false);//查询
while (temp.MoveNext())
{
Console.WriteLine(temp.Current.Name + "\t" + temp.Current.Value);
temp.Current.InsertBefore("<插入子节点>李志伟</插入子节点>");//插入节点
}
}
xml.Save(@"..\..\XML\XML.xml");
Console.WriteLine("OK!");
Console.Read();
}
}
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:template match="/">
<html>
<head><title>李志伟</title></head>
<body><table border="1"><xsl:apply-templates/></table></body>
</html>
</xsl:template>
<xsl:template match="根节点">
<xsl:apply-templates select="节点"/>
</xsl:template>
<xsl:template match="节点">
<tr>
<td><xsl:apply-templates select="子节点1"/></td>
<td><xsl:apply-templates select="子节点2"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<根节点>
<节点>
<子节点1>第一列01</子节点1>
<子节点2>第二列01</子节点2>
</节点>
<节点>
<子节点1>第一列02</子节点1>
<子节点2>第二列02</子节点2>
</节点>
</根节点>
class Program
{
static void Main(string[] args)
{
XslCompiledTransform trans = new XslCompiledTransform();
trans.Load(@"..\..\XML\XSLTFile.xslt");//加载XSLT文件
trans.Transform(@"..\..\XML\XML.xml", @"..\..\XML\HTML.html");//转换HTML文件
Console.WriteLine("OK!");
Console.Read();
}
}
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:Test="urn:XslTest">
<xsl:template match="/">
<html>
<head><title>李志伟</title></head>
<body><table border="1"><xsl:apply-templates/></table></body>
</html>
</xsl:template>
<xsl:template match="根节点">
<xsl:apply-templates select="节点"/>
</xsl:template>
<xsl:template match="节点">
<tr>
<td><xsl:apply-templates select="子节点1"/></td>
<td><xsl:apply-templates select="子节点2"/></td>
<td><xsl:value-of select="Test:ShowText()"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
class Test
{
private int Count = 0;
public string ShowText()
{
return "李志伟,行数:" + (Count++);
}
}
class Program
{
static void Main(string[] args)
{
XPathDocument xml = new XPathDocument(@"..\..\XML\XML.xml");
XslCompiledTransform trans = new XslCompiledTransform();
trans.Load(@"..\..\XML\XSLTFile.xslt");//加载XSLT文件
XmlWriter xw = new XmlTextWriter(@"..\..\XML\HTML.html", Encoding.UTF8);
XsltArgumentList arg = new XsltArgumentList();
Test test = new Test();
arg.AddExtensionObject("urn:XslTest", test);//绑定对象参数
XPathNavigator nav = xml.CreateNavigator();
trans.Transform(nav, arg, xw);//生成文件
xw.Close();
Console.WriteLine("OK!");
Console.Read();
}
}
public class Test//被序列化对象
{
[XmlAttribute]
public string ID { set; get; }
[XmlElement]
public string Name { set; get; }
[XmlElement]
public int Age { set; get; }
[XmlElement]
public string Sex { set; get; }
[XmlElement]
public List<string> Interest { set; get; }
public override string ToString()
{
return "ID:" + ID + ",Name" + Name + ",Age" + Age + ",Sex" + Sex;
}
}
class Program
{
static void Main(string[] args)
{
//创建对象
Test test = new Test() { ID = "01", Name = "李志伟", Age = 20, Sex = "男" };
test.Interest = new List<string>();
test.Interest.Add("看电影");
test.Interest.Add("听歌");
test.Interest.Add("看书");
//序列化
XmlTextWriter xmlw = new XmlTextWriter(@"..\..\XML\XML.xml", Encoding.UTF8);
XmlSerializer xmlser = new XmlSerializer(typeof(Test));
xmlser.Serialize(xmlw, test);//序列化
xmlw.Close();//关闭文件
Console.WriteLine("序列化成功,请查看XML文件!");
Console.ReadLine();
//反序列化
XmlTextReader xmlr = new XmlTextReader(@"..\..\XML\XML.xml");
Test temp = (Test)xmlser.Deserialize(xmlr);//反序列化
xmlr.Close();
Console.WriteLine("反序列化结果:" + temp);
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
XDocument xml = XDocument.Load(@"..\..\XML\XML.xml"); ;
Console.WriteLine(xml.Root.Name);//根节点名
Console.WriteLine(xml.Root.LastAttribute);//最后一个属性
XNamespace ns = "http://www.cnblogs.com/LiZhiW/";
XElement root = new XElement(ns + "根节点",
new XElement("节点", "节点内容"),
new XElement("节点",
new XElement("子节点", "子节点内容AAA"),
new XElement("子节点", "子节点内容BBB")));
XComment com = new XComment("标准XML注释");//XML注释
root.Add(com);//加入XML注释
XAttribute atu = new XAttribute("作者","李志伟");//XML属性
root.Add(atu);//加入XML属性
root.Save(@"..\..\XML\XML.xml");
Console.WriteLine("OK!");
Console.Read();
}
}
<?xml version="1.0" encoding="utf-8"?>
<根节点>
<节点>
<子节点1>第一列01</子节点1>
<子节点2>第二列01</子节点2>
</节点>
<节点>
<子节点1>第一列02</子节点1>
<子节点2>第二列02</子节点2>
</节点>
</根节点>
class Program
{
static void Main(string[] args)
{
XDocument xml = XDocument.Load(@"..\..\XML\XML.xml"); ;
var query = from element in xml.Descendants("子节点2") select element.Value;
foreach (string s in query)
{
Console.WriteLine(s);
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
XDocument xml = XDocument.Load(@"http://geekswithblogs.net/evjen/Rss.aspx");
Console.WriteLine("已加载动态的XML");
var query = from element in xml.Descendants("title") select element.Value;
foreach (string s in query)
{
Console.WriteLine(s);
}
Console.WriteLine("OK!");
Console.Read();
}
}
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4316589.html