本文整理汇总了C#中System.Xml.XmlReader.ResolveEntity方法的典型用法代码示例。如果您正苦于以下问题:C# XmlReader.ResolveEntity方法的具体用法?C# XmlReader.ResolveEntity怎么用?C# XmlReader.ResolveEntity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlReader
的用法示例。
在下文中一共展示了XmlReader.ResolveEntity方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildSubtree
internal void BuildSubtree(XmlReader reader, XmlWriter writer)
{
string ns = "http://www.w3.org/2000/xmlns/";
ReadState readState = reader.ReadState;
if ((readState != ReadState.Initial) && (readState != ReadState.Interactive))
{
throw new ArgumentException(Res.GetString("Xml_InvalidOperation"), "reader");
}
int num = 0;
if (readState == ReadState.Initial)
{
if (!reader.Read())
{
return;
}
num++;
}
do
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
{
writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
bool isEmptyElement = reader.IsEmptyElement;
while (reader.MoveToNextAttribute())
{
if (reader.NamespaceURI == ns)
{
if (reader.Prefix.Length == 0)
{
writer.WriteAttributeString("", "xmlns", ns, reader.Value);
}
else
{
writer.WriteAttributeString("xmlns", reader.LocalName, ns, reader.Value);
}
}
else
{
writer.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteString(reader.Value);
writer.WriteEndAttribute();
}
}
reader.MoveToElement();
if (isEmptyElement)
{
writer.WriteEndElement();
}
else
{
num++;
}
break;
}
case XmlNodeType.Attribute:
if (reader.NamespaceURI != ns)
{
writer.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteString(reader.Value);
writer.WriteEndAttribute();
break;
}
if (reader.Prefix.Length != 0)
{
writer.WriteAttributeString("xmlns", reader.LocalName, ns, reader.Value);
break;
}
writer.WriteAttributeString("", "xmlns", ns, reader.Value);
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
writer.WriteString(reader.Value);
break;
case XmlNodeType.EntityReference:
reader.ResolveEntity();
break;
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.LocalName, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
writer.WriteString(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
num--;
break;
}
}
//.........这里部分代码省略.........
示例2: ReadNodeCore
//.........这里部分代码省略.........
ReadAttributeNode (reader));
reader.MoveToElement ();
}
// FIXME: the code below should be fine and
// in some XmlReaders it is much faster, but
// caused some breakage in sys.data test.
/*
if (reader.MoveToFirstAttribute ()) {
do {
element.SetAttributeNode (ReadAttributeNode (reader));
} while (reader.MoveToNextAttribute ());
reader.MoveToElement ();
}
*/
reader.MoveToElement ();
int depth = reader.Depth;
if (reader.IsEmptyElement) {
n = element;
break;
}
reader.Read ();
while (reader.Depth > depth) {
n = ReadNodeCore (reader);
if (preserveWhitespace || n.NodeType != XmlNodeType.Whitespace)
element.AppendChild (n, false);
}
n = element;
break;
case XmlNodeType.ProcessingInstruction:
n = CreateProcessingInstruction (reader.Name, reader.Value);
break;
case XmlNodeType.Text:
n = CreateTextNode (reader.Value);
break;
case XmlNodeType.XmlDeclaration:
n = CreateXmlDeclaration ("1.0" , String.Empty, String.Empty);
n.Value = reader.Value;
break;
case XmlNodeType.DocumentType:
DTDObjectModel dtd = null;
IHasXmlParserContext ctxReader = reader as IHasXmlParserContext;
if (ctxReader != null)
dtd = ctxReader.ParserContext.Dtd;
if (dtd != null)
n = CreateDocumentType (dtd);
else
n = CreateDocumentType (reader.Name, reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
break;
case XmlNodeType.EntityReference:
if (this.loadMode && this.DocumentType != null &&
DocumentType.Entities.GetNamedItem (reader.Name) == null)
throw new XmlException ("Reference to undeclared entity was found.");
n = CreateEntityReference (reader.Name);
// IF argument XmlReader can resolve entity,
// ReadNode() also fills children _from it_.
// In this case, it is not from doctype node.
// (it is kind of sucky design, but it happens
// anyways when we modify doctype node).
//
// It does not happen when !CanResolveEntity.
// (In such case AppendChild() will resolve
// entity content, as XmlEntityReference does.)
if (reader.CanResolveEntity)
{
reader.ResolveEntity ();
reader.Read ();
for (XmlNode child; reader.NodeType != XmlNodeType.EndEntity && (child = ReadNode (reader)) != null;)
n.InsertBefore (child, null, false, false);
}
break;
case XmlNodeType.SignificantWhitespace:
n = CreateSignificantWhitespace (reader.Value);
break;
case XmlNodeType.Whitespace:
n = CreateWhitespace (reader.Value);
break;
case XmlNodeType.None:
return null;
default:
// No idea why MS does throw NullReferenceException ;-P
throw new NullReferenceException ("Unexpected node type " + reader.NodeType + ".");
}
reader.Read ();
return n;
}
示例3: fromEntity
private string fromEntity(XmlReader xr)
{
xr.Read();
if (xr.NodeType == XmlNodeType.EntityReference) {
string name = xr.Name;
if (!entities.ContainsKey(name)) {
xr.ResolveEntity();
entities.Add(name, xr.ReadString());
}
return name;
} else {
throw new MyException("Wtf?" + xr.NodeType.ToString() + ": " + xr.Name + ": " + xr.ReadString());
}
}
示例4: LoadFromReader
internal void LoadFromReader(XmlReader reader, XmlSpace space)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
IXmlLineInfo lineInfo = reader as IXmlLineInfo;
if ((lineInfo == null) || !lineInfo.HasLineInfo())
{
lineInfo = null;
}
this.hasLineInfo = lineInfo != null;
this.nameTable = reader.NameTable;
XPathDocumentBuilder builder = new XPathDocumentBuilder(this, lineInfo, reader.BaseURI, LoadFlags.None);
try
{
bool isEmptyElement;
string str2;
bool flag = reader.ReadState == ReadState.Initial;
int depth = reader.Depth;
string str = this.nameTable.Get("http://www.w3.org/2000/xmlns/");
if (flag && !reader.Read())
{
return;
}
Label_007D:
if (!flag && (reader.Depth < depth))
{
return;
}
switch (reader.NodeType)
{
case XmlNodeType.Element:
isEmptyElement = reader.IsEmptyElement;
builder.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.BaseURI);
goto Label_017B;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
builder.WriteString(reader.Value, TextBlockType.Text);
goto Label_022B;
case XmlNodeType.EntityReference:
reader.ResolveEntity();
goto Label_022B;
case XmlNodeType.ProcessingInstruction:
builder.WriteProcessingInstruction(reader.LocalName, reader.Value, reader.BaseURI);
goto Label_022B;
case XmlNodeType.Comment:
builder.WriteComment(reader.Value);
goto Label_022B;
case XmlNodeType.DocumentType:
{
IDtdInfo dtdInfo = reader.DtdInfo;
if (dtdInfo != null)
{
builder.CreateIdTables(dtdInfo);
}
goto Label_022B;
}
case XmlNodeType.Whitespace:
goto Label_01C9;
case XmlNodeType.SignificantWhitespace:
if (reader.XmlSpace != XmlSpace.Preserve)
{
goto Label_01C9;
}
builder.WriteString(reader.Value, TextBlockType.SignificantWhitespace);
goto Label_022B;
case XmlNodeType.EndElement:
builder.WriteEndElement(false);
goto Label_022B;
default:
goto Label_022B;
}
Label_0113:
str2 = reader.NamespaceURI;
if (str2 == str)
{
if (reader.Prefix.Length == 0)
{
builder.WriteNamespaceDeclaration(string.Empty, reader.Value);
}
else
{
builder.WriteNamespaceDeclaration(reader.LocalName, reader.Value);
}
}
else
{
builder.WriteStartAttribute(reader.Prefix, reader.LocalName, str2);
builder.WriteString(reader.Value, TextBlockType.Text);
builder.WriteEndAttribute();
}
//.........这里部分代码省略.........
示例5: FindNodeType
public bool FindNodeType(XmlReader r, XmlNodeType _nodetype)
{
if (r.NodeType == _nodetype)
return false;
while (r.Read())
{
if (r.NodeType == XmlNodeType.EntityReference)
{
if (r.CanResolveEntity)
r.ResolveEntity();
}
if (r.NodeType == XmlNodeType.ProcessingInstruction && r.NodeType == XmlNodeType.XmlDeclaration)
{
if (String.Compare(r.Name, 0, ST_XML, 0, 3) != 0)
return true;
}
if (r.NodeType == _nodetype)
{
return true;
}
if (r.NodeType == XmlNodeType.Element && (_nodetype == XmlNodeType.Attribute))
{
if (r.MoveToFirstAttribute())
{
return true;
}
}
}
return false;
}
示例6: ReadChildNodes
//.........这里部分代码省略.........
case XmlNodeType.SignificantWhitespace:
if (reader.XmlSpace == XmlSpace.Preserve)
{
// If we're preserving whitespace, try to append it to a text node instead
// of creating a new node
n = LastChild(parent);
if (n != NullIndex && (this.nodes[n].type == XPathNodeType.Text ||
this.nodes[n].type == XPathNodeType.Whitespace ||
this.nodes[n].type == XPathNodeType.SignificantWhitespace))
{
this.nodes[n].val = this.nodes[n].val + reader.Value;
}
else
{
n = NewNode();
this.nodes[n].type = XPathNodeType.SignificantWhitespace;
this.nodes[n].val = reader.Value;
this.nodes[n].baseUri = reader.BaseURI;
this.nodes[n].xmlLang = reader.XmlLang;
AddChild(parent, n);
}
}
else
{
goto case XmlNodeType.Whitespace;
}
break;
case XmlNodeType.Whitespace:
if (this.space == XmlSpace.Preserve)
{
// If we're preserving whitespace, try to append it to a text node instead
// of creating a new node
n = LastChild(parent);
if (n != NullIndex && (this.nodes[n].type == XPathNodeType.Text ||
this.nodes[n].type == XPathNodeType.Whitespace ||
this.nodes[n].type == XPathNodeType.SignificantWhitespace))
{
this.nodes[n].val = this.nodes[n].val + reader.Value;
}
else
{
n = NewNode();
this.nodes[n].type = XPathNodeType.Whitespace;
this.nodes[n].val = reader.Value;
this.nodes[n].baseUri = reader.BaseURI;
this.nodes[n].xmlLang = reader.XmlLang;
AddChild(parent, n);
}
}
break;
case XmlNodeType.CDATA:
case XmlNodeType.Text:
// Try to append it to a text node instead of creating a new node
n = LastChild(parent);
if (n == NullIndex || (this.nodes[n].type != XPathNodeType.Text &&
this.nodes[n].type != XPathNodeType.Whitespace &&
this.nodes[n].type != XPathNodeType.SignificantWhitespace))
{
n = NewNode();
this.nodes[n].baseUri = reader.BaseURI;
this.nodes[n].xmlLang = reader.XmlLang;
AddChild(parent, n);
}
this.nodes[n].type = XPathNodeType.Text;
this.nodes[n].val = reader.Value;
break;
case XmlNodeType.EntityReference:
reader.ResolveEntity();
reader.Read();
ReadChildNodes(reader, parent, parentNS);
break;
case XmlNodeType.EndEntity:
case XmlNodeType.EndElement:
case XmlNodeType.None:
return n;
case XmlNodeType.DocumentType:
break;
case XmlNodeType.XmlDeclaration:
default:
break;
}
} while (reader.Read());
return n;
}
示例7: BuildSubtree
internal void BuildSubtree(XmlReader reader, XmlWriter writer) {
// important (perf) string literal...
string xmlnsUri = XmlReservedNs.NsXmlNs; // http://www.w3.org/2000/xmlns/
ReadState readState = reader.ReadState;
if (readState != ReadState.Initial
&& readState != ReadState.Interactive) {
throw new ArgumentException(Res.GetString(Res.Xml_InvalidOperation), "reader");
}
int level = 0;
if ( readState == ReadState.Initial ) {
if( !reader.Read() )
return;
level++; // if start in initial, read everything (not just first)
}
do {
switch (reader.NodeType) {
case XmlNodeType.Element:
writer.WriteStartElement( reader.Prefix, reader.LocalName, reader.NamespaceURI );
bool isEmptyElement = reader.IsEmptyElement;
while (reader.MoveToNextAttribute()) {
if ((object) reader.NamespaceURI == (object) xmlnsUri) {
if (reader.Prefix.Length == 0) {
// Default namespace declaration "xmlns"
Debug.Assert(reader.LocalName == "xmlns");
writer.WriteAttributeString( "", "xmlns", xmlnsUri, reader.Value );
}
else {
Debug.Assert(reader.Prefix == "xmlns");
writer.WriteAttributeString( "xmlns", reader.LocalName, xmlnsUri, reader.Value );
}
}
else {
writer.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteString(reader.Value);
writer.WriteEndAttribute();
}
}
reader.MoveToElement();
if (isEmptyElement) {
// there might still be a value, if there is a default value specified in the schema
writer.WriteEndElement();
}
else {
level++;
}
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
//should not read beyond the level of the reader's original position.
level--;
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
writer.WriteString( reader.Value );
break;
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
//
writer.WriteString( reader.Value );
break;
case XmlNodeType.Comment:
writer.WriteComment( reader.Value );
break;
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction( reader.LocalName , reader.Value);
break;
case XmlNodeType.EntityReference:
reader.ResolveEntity(); //
break;
case XmlNodeType.EndEntity:
case XmlNodeType.None:
case XmlNodeType.DocumentType:
case XmlNodeType.XmlDeclaration:
break;
case XmlNodeType.Attribute:
if ((object) reader.NamespaceURI == (object) xmlnsUri) {
if (reader.Prefix.Length == 0) {
// Default namespace declaration "xmlns"
Debug.Assert(reader.LocalName == "xmlns");
writer.WriteAttributeString( "", "xmlns", xmlnsUri, reader.Value );
}
else {
Debug.Assert(reader.Prefix == "xmlns");
writer.WriteAttributeString( "xmlns", reader.LocalName, xmlnsUri, reader.Value );
}
}
else {
writer.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteString(reader.Value);
writer.WriteEndAttribute();
}
break;
}
}
while( reader.Read() && ( level > 0 ) );
}
示例8: LoadFromReader
//.........这里部分代码省略.........
// Read past Initial state; if there are no more events then load is complete
if (topLevelReader && !reader.Read())
return;
// Read all events
do {
// If reader began in intermediate state, return when all siblings have been read
if (!topLevelReader && reader.Depth < initialDepth)
return;
switch (reader.NodeType) {
case XmlNodeType.Element: {
bool isEmptyElement = reader.IsEmptyElement;
builder.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.BaseURI);
// Add attribute and namespace nodes to element
while (reader.MoveToNextAttribute()) {
string namespaceUri = reader.NamespaceURI;
if ((object) namespaceUri == (object) xmlnsUri) {
if (reader.Prefix.Length == 0) {
// Default namespace declaration "xmlns"
Debug.Assert(reader.LocalName == "xmlns");
builder.WriteNamespaceDeclaration(string.Empty, reader.Value);
}
else {
Debug.Assert(reader.Prefix == "xmlns");
builder.WriteNamespaceDeclaration(reader.LocalName, reader.Value);
}
}
else {
builder.WriteStartAttribute(reader.Prefix, reader.LocalName, namespaceUri);
builder.WriteString(reader.Value, TextBlockType.Text);
builder.WriteEndAttribute();
}
}
if (isEmptyElement)
builder.WriteEndElement(true);
break;
}
case XmlNodeType.EndElement:
builder.WriteEndElement(false);
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
builder.WriteString(reader.Value, TextBlockType.Text);
break;
case XmlNodeType.SignificantWhitespace:
if (reader.XmlSpace == XmlSpace.Preserve)
builder.WriteString(reader.Value, TextBlockType.SignificantWhitespace);
else
// Significant whitespace without xml:space="preserve" is not significant in XPath/XQuery data model
goto case XmlNodeType.Whitespace;
break;
case XmlNodeType.Whitespace:
Debug.Assert(reader.XmlSpace != XmlSpace.Preserve);
// Always filter top-level whitespace
if (space == XmlSpace.Preserve && (!topLevelReader || reader.Depth != 0))
builder.WriteString(reader.Value, TextBlockType.Whitespace);
break;
case XmlNodeType.Comment:
builder.WriteComment(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
builder.WriteProcessingInstruction(reader.LocalName, reader.Value, reader.BaseURI);
break;
case XmlNodeType.EntityReference:
reader.ResolveEntity();
break;
case XmlNodeType.DocumentType:
// Create ID tables
SchemaInfo info = XmlReader.GetDtdSchemaInfo(reader);
if (info != null)
builder.CreateIdTables(info);
break;
case XmlNodeType.EndEntity:
case XmlNodeType.None:
case XmlNodeType.XmlDeclaration:
break;
}
}
while (reader.Read());
}
finally {
builder.Close();
}
}
示例9: ReadChildNodes
private int ReadChildNodes(XmlReader reader, int parent, int parentNS)
{
int index = 0;
Label_0002:
switch (reader.NodeType)
{
case XmlNodeType.None:
case XmlNodeType.EndElement:
case XmlNodeType.EndEntity:
return index;
case XmlNodeType.Element:
index = this.NewNode();
this.nodes[index].type = XPathNodeType.Element;
this.nodes[index].prefix = reader.Prefix;
this.nodes[index].name = reader.LocalName;
this.nodes[index].ns = reader.NamespaceURI;
this.nodes[index].firstNamespace = parentNS;
this.nodes[index].baseUri = reader.BaseURI;
this.nodes[index].xmlLang = reader.XmlLang;
if (reader.IsEmptyElement)
{
this.ReadAttributes(index, reader);
this.nodes[index].empty = true;
break;
}
this.ReadAttributes(index, reader);
reader.Read();
this.ReadChildNodes(reader, index, this.nodes[index].firstNamespace);
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
index = this.LastChild(parent);
if ((index == 0) || (((this.nodes[index].type != XPathNodeType.Text) && (this.nodes[index].type != XPathNodeType.Whitespace)) && (this.nodes[index].type != XPathNodeType.SignificantWhitespace)))
{
index = this.NewNode();
this.nodes[index].baseUri = reader.BaseURI;
this.nodes[index].xmlLang = reader.XmlLang;
this.AddChild(parent, index);
}
this.nodes[index].type = XPathNodeType.Text;
this.nodes[index].val = reader.Value;
goto Label_04EB;
case XmlNodeType.EntityReference:
reader.ResolveEntity();
reader.Read();
this.ReadChildNodes(reader, parent, parentNS);
goto Label_04EB;
case XmlNodeType.ProcessingInstruction:
index = this.NewNode();
this.nodes[index].type = XPathNodeType.ProcessingInstruction;
this.nodes[index].name = reader.LocalName;
this.nodes[index].val = reader.Value;
this.nodes[index].baseUri = reader.BaseURI;
this.nodes[index].xmlLang = reader.XmlLang;
this.AddChild(parent, index);
goto Label_04EB;
case XmlNodeType.Comment:
index = this.NewNode();
this.nodes[index].type = XPathNodeType.Comment;
this.nodes[index].val = reader.Value;
this.nodes[index].baseUri = reader.BaseURI;
this.nodes[index].xmlLang = reader.XmlLang;
this.AddChild(parent, index);
goto Label_04EB;
case XmlNodeType.Whitespace:
goto Label_0331;
case XmlNodeType.SignificantWhitespace:
if (reader.XmlSpace != XmlSpace.Preserve)
{
goto Label_0331;
}
index = this.LastChild(parent);
if ((index == 0) || (((this.nodes[index].type != XPathNodeType.Text) && (this.nodes[index].type != XPathNodeType.Whitespace)) && (this.nodes[index].type != XPathNodeType.SignificantWhitespace)))
{
index = this.NewNode();
this.nodes[index].type = XPathNodeType.SignificantWhitespace;
this.nodes[index].val = reader.Value;
this.nodes[index].baseUri = reader.BaseURI;
this.nodes[index].xmlLang = reader.XmlLang;
this.AddChild(parent, index);
}
else
{
this.nodes[index].val = this.nodes[index].val + reader.Value;
}
goto Label_04EB;
default:
goto Label_04EB;
}
this.AddChild(parent, index);
goto Label_04EB;
Label_0331:
//.........这里部分代码省略.........
示例10: Build
internal XDocument Build(XmlReader xmlReader)
{
XDocument doc = null;
_parent = null;
_root = null;
_prevNode = null;
_lastReadElementEmpty = false;
_lastTextRange = null;
// Enable getting line and column info
var lineInfo = xmlReader as IXmlLineInfo;
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Element:
ProcessElement(xmlReader, lineInfo);
break;
case XmlNodeType.EndElement:
ProcessEndElement(lineInfo);
break;
case XmlNodeType.Text:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
UpdateOpenTagEndInfo(
lineInfo.LineNumber,
lineInfo.LinePosition - TextOffset);
_prevNode = new XText(xmlReader.Value);
UpdateTextRange(_prevNode, lineInfo);
if (_parent != null)
{
_parent.Add(_prevNode);
}
break;
case XmlNodeType.CDATA:
UpdateOpenTagEndInfo(
lineInfo.LineNumber,
lineInfo.LinePosition - CDataOffset);
_prevNode = new XCData(xmlReader.Value);
UpdateTextRange(_prevNode, lineInfo);
if (_parent != null)
{
_parent.Add(_prevNode);
}
break;
case XmlNodeType.Comment:
UpdateOpenTagEndInfo(
lineInfo.LineNumber,
lineInfo.LinePosition - CommentOffset);
_prevNode = new XComment(xmlReader.Value);
UpdateTextRange(_prevNode, lineInfo);
if (_parent != null)
{
_parent.Add(_prevNode);
}
break;
case XmlNodeType.ProcessingInstruction:
UpdateOpenTagEndInfo(
lineInfo.LineNumber,
lineInfo.LinePosition - ProcessingInstructionOffset);
_prevNode = new XProcessingInstruction(
xmlReader.Name, xmlReader.Value);
UpdateTextRange(_prevNode, lineInfo);
if (_parent != null)
{
_parent.Add(_prevNode);
}
break;
case XmlNodeType.EntityReference:
xmlReader.ResolveEntity();
break;
case XmlNodeType.DocumentType:
break;
case XmlNodeType.XmlDeclaration:
break;
case XmlNodeType.EndEntity:
break;
default:
throw new InvalidOperationException(Resources.TreeBuilder_UnknownNodeType);
}
}
doc = new XDocument(_root);
doc.Root.EnsureAnnotation();
return doc;
}
示例11: ReadWithLineInfo
/// <summary>
/// Read an XNode from the given XmlReader and LineInfo object. If available, line info will be added to XElement.
/// This technique is adapted from here: http://blogs.msdn.com/b/mikechampion/archive/2006/09/10/748408.aspx
/// </summary>
/// <param name="reader">The XmlReader to read from</param>
/// <param name="lineInfo">This should be <paramref name="reader"/> cast as an <see cref="IXmlLineInfo"/></param>
/// <returns>an XNode with line information if present</returns>
/// <seealso cref="XNode.ReadFrom">This function replaces XNode.ReadFrom</seealso>
public static XNode ReadWithLineInfo(XmlReader reader, IXmlLineInfo lineInfo)
{
XNode node = null;
XElement parent = null;
do
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
// create a new element with the given name
XElement element = new XElement(XName.Get(reader.LocalName, reader.NamespaceURI));
// add attributes to the element
if (reader.MoveToFirstAttribute())
{
do
{
// Compound documents created with older versions of ABB.SrcML left out some namespaces
// this causes an "xmlns" attribute to be added to any names not in the default (SRC) namespace
// to avoid an ArgumentException thrown by element.Add in this case, just don't add these
if (!(reader.LocalName == "xmlns" && reader.NamespaceURI == "http://www.w3.org/2000/xmlns/"))
element.Add(new XAttribute(XName.Get(reader.LocalName, reader.NamespaceURI), reader.Value));
} while (reader.MoveToNextAttribute());
reader.MoveToElement();
}
// add a ABB.SrcML.LineInfo annotation to the element if line information is present.
if (lineInfo.HasLineInfo())
{
element.SetLineInfo(new LineInfo(lineInfo.LineNumber, lineInfo.LinePosition));
}
// if the reader is not empty, we have to go and get all of the children and add them.
// otherwise, we can jsut set this to node.
if (!reader.IsEmptyElement)
{
if (null != parent)
{
parent.Add(element);
}
parent = element;
continue;
}
else
{
node = element;
}
break;
case XmlNodeType.EndElement:
// process the EndElement
if (null == parent)
return null;
if (parent.IsEmpty)
{
parent.Add(string.Empty);
}
if (parent.Parent == null)
return parent;
parent = parent.Parent;
continue;
case XmlNodeType.Text:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
node = new XText(reader.Value);
break;
case XmlNodeType.CDATA:
node = new XCData(reader.Value);
break;
case XmlNodeType.Comment:
node = new XComment(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
node = new XProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.DocumentType:
node = new XDocumentType(reader.LocalName, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
break;
case XmlNodeType.EntityReference:
reader.ResolveEntity();
continue;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.EndEntity:
continue;
default:
throw new InvalidOperationException();
}
if (null == parent)
return node;
parent.Add(node);
} while (reader.Read());
return null;
//.........这里部分代码省略.........
示例12: ReadContentFrom
internal void ReadContentFrom(XmlReader r, LoadOptions o)
{
if ((o & (LoadOptions.SetLineInfo | LoadOptions.SetBaseUri)) == LoadOptions.None)
{
this.ReadContentFrom(r);
}
else
{
if (r.ReadState != System.Xml.ReadState.Interactive)
{
throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_ExpectedInteractive"));
}
XContainer parent = this;
XNode n = null;
NamespaceCache cache = new NamespaceCache();
NamespaceCache cache2 = new NamespaceCache();
string baseUri = ((o & LoadOptions.SetBaseUri) != LoadOptions.None) ? r.BaseURI : null;
IXmlLineInfo info = ((o & LoadOptions.SetLineInfo) != LoadOptions.None) ? (r as IXmlLineInfo) : null;
do
{
string baseURI = r.BaseURI;
switch (r.NodeType)
{
case XmlNodeType.Element:
{
XElement element = new XElement(cache.Get(r.NamespaceURI).GetName(r.LocalName));
if ((baseUri != null) && (baseUri != baseURI))
{
element.SetBaseUri(baseURI);
}
if ((info != null) && info.HasLineInfo())
{
element.SetLineInfo(info.LineNumber, info.LinePosition);
}
if (r.MoveToFirstAttribute())
{
do
{
XAttribute a = new XAttribute(cache2.Get((r.Prefix.Length == 0) ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
if ((info != null) && info.HasLineInfo())
{
a.SetLineInfo(info.LineNumber, info.LinePosition);
}
element.AppendAttributeSkipNotify(a);
}
while (r.MoveToNextAttribute());
r.MoveToElement();
}
parent.AddNodeSkipNotify(element);
if (!r.IsEmptyElement)
{
parent = element;
if (baseUri != null)
{
baseUri = baseURI;
}
}
break;
}
case XmlNodeType.Text:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
if (((baseUri == null) || (baseUri == baseURI)) && ((info == null) || !info.HasLineInfo()))
{
parent.AddStringSkipNotify(r.Value);
}
else
{
n = new XText(r.Value);
}
break;
case XmlNodeType.CDATA:
n = new XCData(r.Value);
break;
case XmlNodeType.EntityReference:
if (!r.CanResolveEntity)
{
throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnresolvedEntityReference"));
}
r.ResolveEntity();
break;
case XmlNodeType.ProcessingInstruction:
n = new XProcessingInstruction(r.Name, r.Value);
break;
case XmlNodeType.Comment:
n = new XComment(r.Value);
break;
case XmlNodeType.DocumentType:
n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value, r.DtdInfo);
break;
case XmlNodeType.EndElement:
{
if (parent.content == null)
{
//.........这里部分代码省略.........