Path: redb-route/demo/SerialNumbersDemo/SerialNumbers.Core/Integration/Xml/XmlMessageInspector.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/demo/SerialNumbersDemo/SerialNumbers.Core/Integration/Xml/XmlMessageInspector.cs
----
using System.Xml;
namespace SerialNumbers.Core.Integration.Xml;
/// Reads just enough of an incoming file to route it.
public static class XmlMessageInspector
{
private static readonly XmlReaderSettings Settings = new()
{
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null,
IgnoreComments = true,
IgnoreProcessingInstructions = true,
IgnoreWhitespace = true,
};
///
/// The local name of the root element, or null when the content is not well-formed
/// XML. The null is the answer, not a lost error: the intake route records such a file
/// as Invalid, with this reason, and keeps it in the archive.
///
/// The whole document is read, not just its start: a file cut off at the end has a perfectly
/// good root element, and only reading to the last byte tells it from a valid one.
///
///
public static string? RootElementName(string? content)
{
if (string.IsNullOrWhiteSpace(content))
return null;
try
{
using var reader = XmlReader.Create(new StringReader(content), Settings);
if (reader.MoveToContent() != XmlNodeType.Element)
return null;
var root = reader.LocalName;
while (reader.Read())
{
}
return root;
}
catch (XmlException)
{
return null;
}
}
}