开发者

Method to read a XML [closed]

开发者 https://www.devze.com 2023-04-04 06:24 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

i created a method to read a xml file but it doesn't work twice, i have to place the pointer at start of the file but I didn't found how.

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


if you execute your code twice, the second time you'll an exception telling you that the file is opened by anothe process.

So that means that you didn't ... close it. So close it !


Also, look at the Microsoft Support Article - How to read XML file by using Visual C# which has the following example.

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

精彩评论

暂无评论...
验证码 换一张
取 消