zoukankan      html  css  js  c++  java
  • xml操作类

    public class ManagementXml
    ...{
    public ManagementXml()
    ...{
    }
    /**////
    /// 添加元素
    ///

    public void AddElement()
    ...{
    System.Xml.XmlNode newElement;
    newElement = xml.CreateElement("Keyword");
    newElement.InnerText = "Value";
    xml.AppendChild(newElement);
    }
    /**////
    /// 列举所有属性
    ///

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

    }
    }
    /**////
    /// 查到节点名
    ///

    public void Find_A_Node()
    ...{
    System.Xml.XmlNodeList nodes;
    nodes = xml.GetElementsByTagName("ElementName");
    }
    /**////
    /// 从Xml文件中提出模式
    ///

    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);
    }

    }
    /**////
    /// 重新声明已命名节点
    ///

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

    }
    }
    /**////
    /// 用XPathNavigator导航数据
    ///

    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())
    ...{

    }
    }
    /**////
    /// 从Xml中读取数据到类
    ///

    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);
    }
    }
    /**////
    /// 用XmlTextReader读取数据
    ///

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

    string contents = reader.ReadInnerXml();
    }
    /**////
    /// 从字符串读取Xml
    ///

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

    while (reader.Read())
    ...{

    }
    }
    /**////
    /// 从Url读取Xml
    ///

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

    while (reader.Read())
    ...{

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

    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);
    }
    /**////
    /// 用XSl sheet将XMl转化为Html
    ///

    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);
    }
    }
    /**////
    /// 用XSlT将XMl转化为Html
    ///

    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");
    }
    /**////
    /// 将类的数据写入Xml
    ///

    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);
    }
    }

    }
  • 相关阅读:
    24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
    61. Rotate List(M);19. Remove Nth Node From End of List(M)
    素数筛选法(prime seive)
    哈夫曼树;二叉树;二叉排序树(BST)
    sort与qsort的区别与联系
    贪心算法
    First non-repeating character in a stream
    transform
    C++11 & C++14 & C++17新特性
    开个玩笑
  • 原文地址:https://www.cnblogs.com/darejoy/p/1319944.html
Copyright © 2011-2022 走看看