-2

我对一些 XML 文件有疑问。

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

<Maps>

<Location> 

</Location>

<Locat 

它有点像这样截断。当我尝试在记事本中打开它时,它似乎用完了记事本的整个页面。可能是由于内存问题,因为我有大量数据。是否有任何解决方法。我在虚拟机上运行程序

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

1 回答 1

1

使用 writer 时,请务必Close在完成后使用:

尝试添加这个:

   //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();
}
于 2013-05-27T07:51:11.417 回答