C# 将缩进文本转换为XML
我有一个基于文本的文件,其缩进表示XML文件的每个XML标记 如何将此文本转换为C#中的示例XML?C# 将缩进文本转换为XML,c#,xml,schema,C#,Xml,Schema,我有一个基于文本的文件,其缩进表示XML文件的每个XML标记 如何将此文本转换为C#中的示例XML? 我有点迷路了。我必须数一数空格,然后在列表中回过头来确定何时应该关闭标记 sampleroot rootHeader miscInformation
我有点迷路了。我必须数一数空格,然后在列表中回过头来确定何时应该关闭标记
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
以下代码适用于缩进为四个空格的输入文档(请仔细查看输入文档)。这只是一个例子:当然,您可以通过制表符缩进实现对输入文档的支持
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;
}
}
}
输入文件:
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>
输出文件:
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>
请发布您的代码,或者在中更具描述性。您能为示例添加预期的输出XML文档吗?@user1354345,我添加了另一个答案。@user1354345,谢谢。我希望您能够实现选项卡支持等功能。:)从技术上讲,我使用trideceth PSEDOO代码作为解决方案的基础。我编写了一个for循环,并将堆栈与if语句结合使用,for=、>和less-then。这在弹出、添加和打印方面效果很好。