方法读取XML
本文关键字:XML 读取 方法 | 更新日期: 2023-09-27 18:08:11
我创建了一个方法来读取xml文件,但它不工作两次,我必须将指针放在文件的开头,但我没有发现如何。
using Microsoft.AnalysisServices.AdomdClient;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
class Class {
private System.Xml.XmlReader XML_File;
public void DebugXML()
{
this.XML_File.Read();
while (!this.XML_File.EOF)
{
Debug.WriteLine(this.XML_File.ReadOuterXml());
}
}
public Class()
{
AdomdConnection conn = new AdomdConnection("Data Source=MyComputer;InitialCatalog=Database");
conn.Open();
AdomdCommand cmd = new AdomdCommand("Select Hierarchize([Projects].[Project Branch].Levels(1).Members) DIMENSION PROPERTIES PARENT_UNIQUE_NAME, HIERARCHY_UNIQUE_NAME, CUSTOM_ROLLUP, UNARY_OPERATOR, KEY0 ON 0, Hierarchize({{{[Period Calculations].[Period].&[0]}, {[Period Calculations].[Period].&[1]}, {[Period Calculations].[Period].&[2]}, {[Period Calculations].[Period].&[3]}, {[Period Calculations].[Period].&[4]}, {[Period Calculations].[Period].&[5]}}}) DIMENSION PROPERTIES PARENT_UNIQUE_NAME, HIERARCHY_UNIQUE_NAME, CUSTOM_ROLLUP, UNARY_OPERATOR, KEY0 ON 1 FROM [ProjectControl] WHERE ([Measures].[WIP]) CELL PROPERTIES BACK_COLOR, CELL_ORDINAL, FORE_COLOR, FONT_NAME, FONT_SIZE, FONT_FLAGS, FORMAT_STRING, VALUE, FORMATTED_VALUE, UPDATEABLE", conn);
CellSet Cellules = cmd.ExecuteCellSet();
this.XML_File = cmd.ExecuteXmlReader();
DebugXML();
DebugXML();
conn.Close();
}
}
如果执行两次代码,第二次会出现异常,告诉您文件被另一个进程打开。
所以这意味着你没有…关闭它。
另外,请参阅Microsoft支持文章-如何使用Visual c#读取XML文件,其中有以下示例。
using System;
using System.Xml;
namespace ReadXMLfromFile
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Console.ReadLine();
}
}
}