【问题标题】:Method to read a XML [closed]读取 XML 的方法 [关闭]
【发布时间】:2011-09-09 12:56:06
【问题描述】:

我创建了一个读取 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();
        }
}

【问题讨论】:

  • 你没有告诉我们XML_File的类型,或者为什么你需要在课堂上保持打开而不是每次都单独阅读。
  • XmlReader 是只转发阅读器。如果您想多次阅读,则需要使用其他结构。
  • System.Xml.XmlReader XML_File;

标签: c# xml methods


【解决方案1】:

如果你执行你的代码两次,第二次你会抛出一个异常,告诉你文件被另一个进程打开了。

所以这意味着你没有......关闭它。所以关闭它!

【讨论】:

  • 当然,我试过了,但后面是一个繁重的查询,所以我想避免每次都执行它。
  • ??你的意思是 ?你想让我做什么 ?你的问题和这个“繁重的查询”之间的联系是什么?
  • 我明确了我的问题,更具体地说,我使用 Web 服务从 Cube(分析服务)中检索数据,我想将 XML 或 Celldata 存储在我可以访问的地方我的 MainApplication。
  • 然后将您的 xml 文件保存在磁盘上并读取此文件(使用 XmlWriter)
【解决方案2】:

另外,请查看Microsoft Support Article - How to read XML file by using Visual C#,其中有以下示例。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多