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       
4

1 回答 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>
于 2013-02-07T06:26:33.883 回答