我正在尝试将文件导入到包含多个xml文件的Excel工作表中。如下所示:
答案 0 :(得分:0)
您只能拥有一条识别线(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string rootTag = "Root";
StreamReader reader = new StreamReader(FILENAME, Encoding.UTF8);
string xml = "";
string inputLine = "";
int lineCount = 0;
while((inputLine = reader.ReadLine()) != null)
{
inputLine = inputLine.Trim();
if(++lineCount > 2)
{
if(inputLine.StartsWith("<?xml") || inputLine.StartsWith("<" + rootTag) || inputLine.StartsWith("</" + rootTag))
{
inputLine = ""; //remove line
}
}
xml += inputLine;
}
//add back closing tag
xml += "</" + rootTag + ">";
XDocument doc = XDocument.Parse(xml);
}
}
}
答案 1 :(得分:0)
考虑从那个大的原始文件中提取单个xml文件。然后,将这些单个文件读入Excel。
以下是提取解决方案的VBA宏:
Sub ExtractXML()
Dim strPath As String, DataLine As String, strOutput As String, strNewPath As String
Dim FileNum As Integer
Dim fso As Object, oFile As Object
Dim i As Long
strPath = "C\Path\To\XMLRawFile.xml"
Set fso = CreateObject("Scripting.FileSystemObject")
FileNum = FreeFile()
Open strPath For Input As #FileNum
strOutput = ""
i = 1
While Not EOF(FileNum)
' READ EACH LINE OF RAW XML FILE
Line Input #FileNum, DataLine
strOutput = strOutput & DataLine & vbNewLine
' OUTPUT INDIVIUDAL FILES AT THE END OF EACH ROOT NODE
If DataLine = "</Root>" Then
strNewPath = "C\Path\To\XMLNewFile" & i & ".xml"
Set oFile = fso.CreateTextFile(strNewPath)
oFile.WriteLine strOutput
oFile.Close
strOutput = ""
Set oFile = Nothing
i = i + 1
End If
Wend
Set fso = Nothing
MsgBox "Successfully extracted raw xml into multiple xml files!", vbInformation
End Sub