将缩进文本转换为XML

3
我有一个基于文本的文件,其中缩进表示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       

请发布你的代码,或在 你尝试过什么? 中描述得更详细一些。 - Brian
请问您能否为这个例子添加期望的输出XML文档? - Sergey Vyacheslavovich Brunov
@user1354345,我已经添加了另一个答案。 - Sergey Vyacheslavovich Brunov
1个回答

4
以下代码适用于具有四个空格缩进的输入文档(请仔细查看输入文档)。这只是一个例子:当然,您可以实现对使用制表符缩进的输入文档的支持。
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>

@user1354345,谢谢。我希望你能实现选项卡支持等功能。 :) - Sergey Vyacheslavovich Brunov
从技术上讲,我使用了trideceth伪代码作为我的解决方案基础。我编写了一个for循环,并结合if语句和堆栈来处理=、>和<。这个方法很好地实现了弹出、添加和打印功能。 - Charles Morrison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接