XML の検証で遊んでいると、C# が長さ 2 文字未満のコロンの前にテキストを含む名前空間プレフィックス名をスローすることに気付きました。
<?xml version="1.0" encoding="utf-16"?><Example xmlns="a:example" />
C# では無効ですが、
<?xml version="1.0" encoding="utf-16"?><Example xmlns="a1:example" />
ではありません?
これは XML 標準の一部ですか、それともここで C# に何か変なものがありますか?
ここにいくつかのサンプルコードがあります。
// Create fake XML.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement("Example", "a:example"));
//xmlDocument.AppendChild(xmlDocument.CreateElement("Example", "a1:example"));
// Display the XML to the user.
using (StringWriter stringWriter = new StringWriter())
{
using (XmlWriter xmlTextWriter = XmlWriter.Create(stringWriter)) { xmlDocument.WriteTo(xmlTextWriter); }
Console.WriteLine(stringWriter.GetStringBuilder().ToString());
}
// Infer schema.
using (Stream stream = new MemoryStream())
{
xmlDocument.Save(stream);
stream.Position = 0;
new XmlSchemaInference().InferSchema(XmlReader.Create(stream));
}
// If we got this far then we are happy.
Console.WriteLine("We are happy");
これにより、XmlSchemaException が生成されます。
The Namespace 'a:example' is an invalid URI.
FormatException の InnerException を使用:
The string 'a:example' is not a valid Uri value.
コメント行に変更すると、コードが機能します。
XmlConvert.ToUri(String s) がスタック トレースにあり、これが原因である可能性があります。ここで逆コンパイルされたソースを見つけて、 Uri.TryCreateへの道をたどりました。おそらく C# は有効な URI スキームを期待していますか?
御時間ありがとうございます :-)。