XML没有完全写好

时间:2013-05-27 07:06:23

标签: c# .net xml

我遇到一些XML文件的问题。

我有大约500多个文本文件,我从中读取并提取数据并将其导出为XML。但是,生成的XML会被中途截断。

<Maps>

<Location> 

</Location>

<Locat 

它有点像这样截断。当我尝试在记事本中打开它时,它似乎耗尽了记事本的整个页面。可能是由于内存问题,因为我有相当多的数据。是否有任何解决方法。我正在VM上运行该程序

private XmlTextWriter writer = new XmlTextWriter("Maps.xml", System.Text.Encoding.UTF8);
        static void Main(string[] args)
        {
            Program prog = new Program();
            String[] filenames = Directory.GetFiles(@"maps_new");
            prog.writer.WriteStartDocument();
            prog.writer.WriteStartElement("Maps");
            foreach (String file in filenames)
            {
                prog.extractToXML(file);
            }
            prog.writer.WriteEndElement();    
            prog.writer.WriteEndDocument();

        }
public void extractToXML(String filename)
        {
            XPathNodeIterator NodeIter;

            XPathDocument xmldoc = new XPathDocument(filename);
            XPathNavigator nav = xmldoc.CreateNavigator();
            String query = "//Schema/@tree";
            NodeIter = nav.Select(query);
            writer.WriteStartElement("file");

            writer.WriteStartAttribute("name");
            writer.WriteString(extractFileName(filename));
            writer.WriteEndAttribute();

            while (NodeIter.MoveNext())
            {
                writer.WriteStartElement("type");
                writer.WriteString(extractFileName(NodeIter.Current.Value.ToString()));
                writer.WriteEndElement();            
            }

            NodeIter = nav.Select("//Location");
            while (NodeIter.MoveNext())
            {
                writer.WriteStartElement("Location");
                writer.WriteString(NodeIter.Current.Value.ToString());
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }

1 个答案:

答案 0 :(得分:1)

使用作家时,请务必在完成后Close

尝试addind this:

   //rest of code omitted
    foreach (String file in filenames)
    {
      prog.extractToXML(file);
    }
    prog.writer.WriteEndElement();    
    prog.writer.WriteEndDocument();

    prog.writer.Flush(); //<-- here
    prog.writer.Close(); //<-- and here

并且更好:使用using语句并使extractToXML静态:

static void Main(string[] args)
{
   using(var writer = new XmlTextWriter("Maps.xml", System.Text.Encoding.UTF8))
   {
     String[] filenames = Directory.GetFiles(@"maps_new");
     writer.WriteStartDocument();
     writer.WriteStartElement("Maps");
     foreach (String file in filenames)
     {
        extractToXML(file, writer);
     }
     writer.WriteEndElement();    
     writer.WriteEndDocument();

     writer.Flush();
     writer.Close();
   }
}
public static void extractToXML(String filename, XmlTextWriter wirter)
{
    XPathNodeIterator NodeIter;

    XPathDocument xmldoc = new XPathDocument(filename);
    XPathNavigator nav = xmldoc.CreateNavigator();
    String query = "//Schema/@tree";
    NodeIter = nav.Select(query);
    writer.WriteStartElement("file");

    writer.WriteStartAttribute("name");
    writer.WriteString(extractFileName(filename));
    writer.WriteEndAttribute();

    while (NodeIter.MoveNext())
    {
        writer.WriteStartElement("type");
        writer.WriteString(extractFileName(NodeIter.Current.Value.ToString()));
        writer.WriteEndElement();            
    }

    NodeIter = nav.Select("//Location");
    while (NodeIter.MoveNext())
    {
        writer.WriteStartElement("Location");
        writer.WriteString(NodeIter.Current.Value.ToString());
        writer.WriteEndElement();
    }

    writer.WriteEndElement();
}