public Dictionary<Guid, Pair<Schema, Stream>> Find(Stream doc, SchemaList schemas)
{
Pair<string, string> root = null;
Dictionary<Guid, Pair<Schema, Stream>> parts = new Dictionary<Guid, Pair<Schema, Stream>>();
using (XmlReader docReader = XmlReader.Create(doc))
MemoryStream newDocStream = new MemoryStream();
using (XmlWriter newDocWriter = XmlWriter.Create(newDocStream))
while (docReader.Read())
if (docReader.NodeType == XmlNodeType.Element)
Pair<string, string> p = new Pair<string, string>(docReader.LocalName, docReader.NamespaceURI);
if (root == null)
root = p;
Schema schema = GetAssociatedSchema(schemas, p);
if (null != schema)
if (p.Left == root.Left && p.Right == root.Right)
foreach (Pair<Schema, Stream> pair in parts.Values)
pair.Right.Close();
parts.Clear();
newDocStream.Close();
parts.Add(Guid.Empty, new Pair<Schema, Stream>(schema, doc));
doc.Position = 0;
return parts;
}
MemoryStream part = new MemoryStream();
using (XmlWriter partWriter = XmlWriter.Create(part))
PipelineUtils.Util.WriteShallowNode(docReader, partWriter);
if (p.Left == docReader.LocalName &&
p.Right == docReader.NamespaceURI &&
docReader.NodeType == XmlNodeType.EndElement)
break;
Guid g = Guid.NewGuid();
parts.Add(g, new Pair<Schema, Stream>(schema, part));
part.Position = 0;
newDocWriter.WriteElementString(r_TagName, g.ToString());
continue;
PipelineUtils.Util.WriteShallowNode(docReader, newDocWriter);
parts.Add(Guid.Empty, new Pair<Schema, Stream>(null, newDocStream));
newDocStream.Position = 0;
public void ValidateXmlDocumentsAgainstSchema(Dictionary<Guid, Pair<Schema, Stream>> dvn,
Dictionary<Schema, XmlSchemaCollection> s)
if (null == dvn)
throw new ArgumentNullException();
if (1 == dvn.Count)
return;
Pair<Schema, Stream> body = dvn[Guid.Empty];
body.Right.Position = 0;
MemoryStream stream = new MemoryStream();
using (XmlReader reader = XmlReader.Create(body.Right))
using (XmlWriter writer = XmlWriter.Create(stream))
while (reader.Read())
if (reader.NodeType == XmlNodeType.Element &&
reader.LocalName == r_TagName)
reader.Read();
Guid g = new Guid(reader.Value);
using (XmlReader partReader = Validate(dvn[g].Right, s[dvn[g].Left]))
reader.Read();//read guid end tag
if (null == partReader)
writer.WriteElementString(r_TagName, g.ToString());
else
do
PipelineUtils.Util.WriteShallowNode(partReader, writer);
while (partReader.Read());
dvn[g].Right.Close();
dvn.Remove(g);
else PipelineUtils.Util.WriteShallowNode(reader, writer);
body.Right.Close();
body.Right = stream;
stream.Position = 0;
private XmlReader Validate(Stream doc, XmlSchemaCollection xsc)
if (null == doc || null == xsc)
return null;
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ValidationType = ValidationType.Schema;
foreach (XmlSchema xs in xsc)
xrs.Schemas.Add(xs);
using (XmlReader reader = XmlReader.Create(doc, xrs))
try
while (reader.Read()) ;
catch (XmlSchemaValidationException)
XmlReader r = XmlReader.Create(doc);
r.MoveToContent();
return r;