私のアプリケーションで使用するカスタムマークアップパーサーを作成しています。 開始タグと終了タグが別々の行になければ、完璧に動作します。カスタムマークアップパーサーが改行を処理していません
例:
<test>This is a test</test>
完璧に動作しますが、
<test>
this
is
a
test
</test>
が空白文字列を返します。
現時点での回避策は、文字列の改行文字として[-n]
を使用し、プログラムで\n\r
に置き換えます。しかし、これは非常に不便です。
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace AsysEditor.Classes
{
/// <summary>
/// Contains the methods needed to parse a simple XML file
/// </summary>
class XMLParser
{
/// <summary>
/// Parses a simple XML file.
/// </summary>
/// <remarks>
/// Does NOT support nested tags.
/// </remarks>
/// <param name="xml">The file to parse</param>
/// <param name="tag">The wanted value</param>
/// <param name="clean">Remove whitespace</param>
/// <param name="replaceNewLines">Replace "[-n]" with "\n\r"</param>
/// <returns></returns>
public static string Parse(string xml, string tag, bool clean, bool replaceNewLines)
{
if (xml == String.Empty || tag == String.Empty) { return "error"; }
if (!(xml.Contains("<" + tag + ">"))) { return "error"; }
// Get all XML tags: <tag>
string _tag = "\\<(.*?)\\>";
MatchCollection tagMatches = new Regex(_tag).Matches(xml);
List<string> tags = new List<string>();
// Add the tag to a list
foreach (Match m in tagMatches)
{
// Clean the tag and add it to the list
tags.Add(m.Groups[1].Value.Replace("<", string.Empty).Replace(">", string.Empty));
}
// Get the value of the tag
foreach (string h in tags)
{
if (!h.Equals(tag)) continue;
string head = "\\<" + h + "\\>";
string foot = "\\</" + h + "\\>";
string contents = new Regex(head + "(.*?)" + foot).Match(xml).Groups[1].Value;
// Clean the result if nessesary
if (clean) return contents.Trim();
else if (replaceNewLines) return contents = Regex.Replace(contents, "\\[-n\\]", "\r\n");
else return contents;
}
return "error";
}
}
}
(それは不必要な多くのことをやっている場合は、その私は、後に機能を拡張するに滑走していますので)
私はここで間違っつもりですどこの誰かが説明できるならば、それは非常に参考になります。
(Also, the entire project is on GitHub)
カスタムxmlプロセッサを使用する理由は何ですか? [Linq to Xml](https://msdn.microsoft.com/en-au/library/mt693072.aspx)は非常に高速で、信頼性が高く、使い方も簡単です。 – Nico
準拠のXMLパーサを書くことはこれよりはるかに難しいですが、それは解決された問題です。ホイールを再構築しないでください。実績のあるXML解析ライブラリを使用します。 – kjhughes
@Nico実際にはXMLではなく、構文タイプを使用しているためです。私が読んでいるファイルは次のとおりです:https://dl.dropboxusercontent.com/u/276558657/Asys/asys.txt正規表現を使うのは簡単なファイルなので、これは簡単です – criticaldiamonds