2013-02-06 44 views
2

我有一个带有缩进的文本文件,它代表XML文件的每个XML标记。将缩进文本转换为XML

我该如何将此文本转换为C#中的示例XML?
我有点失落。我必须计算空格并回顾列表以确定标记何时应该关闭。

sampleroot           
    rootHeader           
    miscInformation         
     Creation         
     DocumentIdentification        
      Identifier       
      Message_Type        
      Notes       
     StandardDocumentationIdentification        
      Standard        
      Version       
    Receiver          
     Name         
     lok        
     Location         
    Sender         
     Name         
     lok2         
    msgref         
     DocumentIdentifier        
     HoldInformation        
      Name        
      Date        
     ReleaseInformation        
      Date        
    HoldDocumentReference         
     AlternativeIdentifier        
      Authority       
      Identifier       
     Notes        
    ReleaseDocumentReference          
     AlternativeIdentifier        
      Authority       
      Identifier       
     Notes  
+0

请发表你的代码,或者是更具描述性的[你有什么试过吗?(http://mattgemmell.com/2008/12/08/what-have-yo U型试过/)。 – Brian

+0

您能否为示例添加预期的输出XML文档? –

+0

@ user1354345,我添加了另一个答案。 –

回答

3

下面的代码工作有具有四个空格压痕(请仔细看一看的输入文档)输入文档。这只是一个例子:当然你可以通过制表符缩进实现对输入文档的支持。

private static void ConvertToXml(Stream inputStream, Stream outputStream) 
{ 
    const int oneIndentLength = 4; // One level indentation - four spaces. 
    var xmlWriterSettings = new XmlWriterSettings 
     { 
      Indent = true 
     }; 

    using (var streamReader = new StreamReader(inputStream)) 
    using (var xmlWriter = XmlWriter.Create(outputStream, xmlWriterSettings)) 
    { 
     int previousIndent = -1; // There is no previous indent. 
     string line; 
     while ((line = streamReader.ReadLine()) != null) 
     { 
      var indent = line.TakeWhile(ch => ch == ' ').Count(); 
      indent /= oneIndentLength; 

      var elementName = line.Trim(); 

      if (indent <= previousIndent) 
      { 
       // The indent is the same or is less than previous one - write end for previous element. 
       xmlWriter.WriteEndElement(); 

       var indentDelta = previousIndent - indent; 
       for (int i = 0; i < indentDelta; i++) 
       { 
        // Return: leave the node. 
        xmlWriter.WriteEndElement(); 
       } 
      } 

      xmlWriter.WriteStartElement(elementName); 

      // Save the indent of the previous line. 
      previousIndent = indent; 
     } 
    } 
} 

客户端代码:

using (var inputStream = File.OpenRead(@"Input file path")) 
using (var outputStream = File.Create(@"Output file path")) 
{ 
    ConvertToXml(inputStream, outputStream); 
} 

输入文档:

sampleroot 
    rootHeader 
     miscInformation 
      Creation 
      DocumentIdentification 
       Identifier 
       Message_Type 
       Notes 
      StandardDocumentationIdentification 
       Standard 
       Version 
     Receiver 
      Name 
      lok 
      Location 
     Sender 
      Name 
      lok2 
     msgref 
      DocumentIdentifier 
      HoldInformation 
       Name 
       Date 
      ReleaseInformation 
       Date 
     HoldDocumentReference 
      AlternativeIdentifier 
       Authority 
       Identifier 
      Notes 
     ReleaseDocumentReference 
      AlternativeIdentifier 
       Authority 
       Identifier 
      Notes 

输出文件:

<?xml version="1.0" encoding="utf-8"?> 
<sampleroot> 
    <rootHeader> 
    <miscInformation> 
     <Creation /> 
     <DocumentIdentification> 
     <Identifier /> 
     <Message_Type /> 
     <Notes /> 
     </DocumentIdentification> 
     <StandardDocumentationIdentification> 
     <Standard /> 
     <Version /> 
     </StandardDocumentationIdentification> 
    </miscInformation> 
    <Receiver> 
     <Name /> 
     <lok /> 
     <Location /> 
    </Receiver> 
    <Sender> 
     <Name /> 
     <lok2 /> 
    </Sender> 
    <msgref> 
     <DocumentIdentifier /> 
     <HoldInformation> 
     <Name /> 
     <Date /> 
     </HoldInformation> 
     <ReleaseInformation> 
     <Date /> 
     </ReleaseInformation> 
    </msgref> 
    <HoldDocumentReference> 
     <AlternativeIdentifier> 
     <Authority /> 
     <Identifier /> 
     </AlternativeIdentifier> 
     <Notes /> 
    </HoldDocumentReference> 
    <ReleaseDocumentReference> 
     <AlternativeIdentifier> 
     <Authority /> 
     <Identifier /> 
     </AlternativeIdentifier> 
     <Notes /> 
    </ReleaseDocumentReference> 
    </rootHeader> 
</sampleroot> 
+0

好吧你赢了:)这就是我正在寻找 –

+0

@ user1354345,谢谢。我希望你能实现标签支持等等。 :) –

+0

技术上,我使用trideceth pseduo代码作为我的解决方案的基础。我写了一个for循环,并使用了一个与if语句组合的堆栈,用于存放=,>和小于这个值。这很好地弹出和添加和打印。 –

1
def count_spaces(s): 
    i = 0 
    for c in s: 
     if c == " ": 
      i += 1 
     else: 
      return i 

lastlevel = 0 
lastheader = "" 
close_stack = [] 
for line in file: 
    level = count_spaces(line) 
    if level == lastlevel: 
     xml += "<"+line+"/>" 
    elif level > lastlevel: 
     xml += "<"+line+"/>" 
     close_stack.push(lastheader) 
     lastheader = line 
    else: 
     xml += "</"+close_stack.pop()+">"