站在巨人肩上,看得更高

输电线路工程,地理信息系统,ASP.NET,C#,VB.NET.WINDOWS MOBILE

用户操作
[即时聊天] [发私信] [加为好友]
lg836ID:lg836
15016次访问,排名8179,好友0人,关注者0人。
lg836的文章
原创 32 篇
翻译 0 篇
转载 6 篇
评论 2 篇
lg836的公告
欢迎大家来多逛逛! 点击这里给我发消息
Google
最近评论
lg836:看看能不能回复
lg836:看看能不能回复
文章分类
收藏
相册
三峡大学
asp.net技术
微软asp.net中心
地理信息系统
慧图
超图
朋友博客
小韦
输电线路工程
电力导航
电力新闻网
输电线路人才网
输电线路工程网
输电线路综合论坛(RSS)
搜索
google
百度
综合搜索
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

原创 C#操作Xml全集(1)收藏

新一篇: XML操作示例(2)----xpath | 旧一篇: 利用AjaxPro框架实现无刷新用户登录验证

 

输电线路设计系统中要保存导线数据,首先想到的是数据来保存,但又想了一下,感觉比较麻烦,数据库移植起来

是很麻烦的事,老是出错,反正是做的二次开发包,简单易用,功能强大是首要的,你不可能要你的二次开发用户去配置半天的数据库吧!

如实想到用Xml来存储一些量不大的数据,如气象条件,导线,绝缘子等等,读取方便,并不需要考虑安全性的问题,Xml同样可以进行复杂查询操作,功能强劲。

昨天到微软msdn上查了查,找到了相关的代码示例,现在贴出来,大家可以下载试用,安装到vs2005智能代码段中,非常方便。

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

            }


    }
 

发表于 @ 2007年06月30日 13:14:00|评论(loading...)|编辑

新一篇: XML操作示例(2)----xpath | 旧一篇: 利用AjaxPro框架实现无刷新用户登录验证

评论:没有评论。

发表评论
  • 姓   名: 
  • 主   页:
  • 校验码: