C#操作Xml全集(1)

来源:互联网 发布:淘宝上新率 编辑:程序博客网 时间:2024/04/30 23:28

http://blog.csdn.net/lg836/archive/2007/06/30/1672476.aspx

public class ManagementXml
    
{
        
public ManagementXml()
        
{
        }

         
/// <summary>
            
/// 添加元素
            
/// </summary>

            public void AddElement()
            
{
                System.Xml.XmlNode newElement;
                newElement 
= xml.CreateElement("Keyword");
                newElement.InnerText 
= "Value";
                xml.AppendChild(newElement);
            }

            
/// <summary>
            
/// 列举所有属性
            
/// </summary>

            public void EnumAttribute()
            
{
                
foreach (System.Xml.XmlAttribute attribute in node.Attributes)
                
{

                }

            }

            
/// <summary>
            
/// 查到节点名
            
/// </summary>

            public void Find_A_Node()
            
{
                System.Xml.XmlNodeList nodes;
                nodes 
= xml.GetElementsByTagName("ElementName");
            }

            
/// <summary>
            
/// 从Xml文件中提出模式
            
/// </summary>

            public void Infer_A_Schema_From_A_Xml()
            
{
                
// Gets the schema.
                System.Xml.Schema.XmlSchemaInference infer = new System.Xml.Schema.XmlSchemaInference();
                System.Xml.Schema.XmlSchemaSet sc 
= new System.Xml.Schema.XmlSchemaSet();
                sc 
= infer.InferSchema(new System.Xml.XmlTextReader("sample.xml"));
                
// Writes the schema.
                System.Xml.XmlWriter w = System.Xml.XmlWriter.Create(new System.IO.StreamWriter("sampleSchema.xsd"));

                
foreach (System.Xml.Schema.XmlSchema schema in sc.Schemas())
                
{
                    schema.Write(w);
                }


                
// Update the schema with another xml fragment.
                sc = infer.InferSchema(System.Xml.XmlReader.Create("anothersample.xml"), sc);
                w 
= System.Xml.XmlWriter.Create(new System.IO.StreamWriter("anotherschema.xsd"));
                
foreach (System.Xml.Schema.XmlSchema schema in sc.Schemas())
                
{
                    schema.Write(w);
                }


            }

            
/// <summary>
            
/// 重新声明已命名节点
            
/// </summary>

            public void Iterate_Named_Nodes()
            
{
                
foreach (System.Xml.XmlNode node in xmlDoc.GetElementsByTagName("ElementName"))
                
{
                    
                }

            }

            
/// <summary>
            
/// 用XPathNavigator导航数据
            
/// </summary>

            public void NavigateData_with_XPathNavigator()
            
{
                System.Xml.XPath.XPathNavigator nav 
= xmlDoc.CreateNavigator();

                
// Move to the first non-comment element.
                nav.MoveToChild(System.Xml.XPath.XPathNodeType.Element);

                System.Xml.XPath.XPathNodeIterator nodeIterator 
= nav.SelectChildren(System.Xml.XPath.XPathNodeType.Element);

                
while (nodeIterator.MoveNext())
                
{
                    
                }

            }

            
/// <summary>
            
/// 从Xml中读取数据到类
            
/// </summary>

            public void Read_ClassData_From_Xml()
            
{
                System.Xml.XmlSerializer reader 
= new System.Xml.XmlSerializer(typeof(TheClass));
                
using (System.IO.StreamReader file = new System.IO.StreamReader(@"ClassData.xml"))
                
{
                    TheClass fileData;

                    fileData 
= (TheClass)reader.Deserialize(file);
                }

            }

            
/// <summary>
            
/// 用XmlTextReader读取数据
            
/// </summary>

            public void Read_Data_With_XmlTextReader()
            
{
                System.Xml.XmlTextReader reader 
= new System.Xml.XmlTextReader("Snippet.xml");

                
string contents = reader.ReadInnerXml();
            }

            
/// <summary>
            
/// 从字符串读取Xml
            
/// </summary>

            public void Read_Xml_Form_String()
            
{
                
// Create the reader.
                System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new System.IO.StringReader("<book>"));

                
while (reader.Read())
                
{
                    
                }

            }

            
/// <summary>
            
/// 从Url读取Xml
            
/// </summary>

            public void ReadXmlFromUrl()
            
{
                
string myUrl = "http://www.contoso.com/books.xml";
                System.Xml.XmlReader reader 
= System.Xml.XmlReader.Create(myUrl);

                
while (reader.Read())
                
{
                    
                }

            }

            
/// <summary>
            
/// 用XPathNavigator读取节点数据,相当与sql查询,当然可以进行更新,查询,删除,添加了
            
/// </summary>

            public void Selet_Nodes_With_XPathNavigator()
            
{
                System.Xml.XPath.XPathExpression myXPathExpr;
                System.Xml.XPath.XPathDocument myXPathDocument 
= new System.Xml.XPath.XPathDocument(@"data.xml");
                
// Create an XPathNavigator.
                System.Xml.XPath.XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
                
// Get the Book elements.          
                string selectExpr = "parent/child";
                
// Ensure we are at the root node.          
                myXPathNavigator.MoveToRoot();
                myXPathExpr 
= myXPathNavigator.Compile(selectExpr);
                
// Create an XPathNodeIterator to walk over the selected nodes.
                System.Xml.XPath.XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select(myXPathExpr);
            }

            
/// <summary>
            
/// 用XSl sheet将XMl转化为Html
            
/// </summary>

            public void Transform_Xml_To_Html_Using_Xsl()
            
{
                
using (System.IO.FileStream stream = System.IO.File.Open("output.html", System.IO.FileMode.Create))
                
{
                    
// Create XsltCommand compile stylesheet.
                    System.Xml.Xsl.XslCompiledTransform processor = new System.Xml.Xsl.XslCompiledTransform();
                    processor.Load(
"stylesheet.xsl");

                    
// Transform the file.
                    processor.Transform("data.xml"null, stream);
                }

            }

            
/// <summary>
            
/// 用XSlT将XMl转化为Html
            
/// </summary>

            public void Transform_Xml_To_Html_Using_Xslt()
            
{
                System.Xml.Xsl.XslCompiledTransform xslt 
= new System.Xml.Xsl.XslCompiledTransform();

                xslt.Load(
"theXsltFile.xslt");
                xslt.Transform(
"theXmlFile.xml""theOutputFile.html");
            }

            
/// <summary>
            
/// 将类的数据写入Xml
            
/// </summary>

            public void WriteClassDataToXml()
            
{
                System.Xml.Serialization.XmlSerializer writer 
= new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.ArrayList));

                
using (System.IO.StreamWriter file = new System.IO.StreamWriter("SerializedData.xml"))
                
{
                    writer.Serialize(file, dataToWrite);
                }

            }


    }