Example #1
0
	public void DumpReader (XmlReader xr, bool attValue)
	{
		Console.WriteLine ("NodeType: " + xr.NodeType);
		Console.WriteLine ("Prefix: " + xr.Prefix);
		Console.WriteLine ("Name: " + xr.Name);
		Console.WriteLine ("LocalName: " + xr.LocalName);
		Console.WriteLine ("NamespaceURI: " + xr.NamespaceURI);
		Console.WriteLine ("Value: " + xr.Value);
		Console.WriteLine ("Depth: " + xr.Depth);
		Console.WriteLine ("IsEmptyElement: " + xr.IsEmptyElement);

		if (xr.NodeType == XmlNodeType.Attribute) {
			Console.WriteLine ("Attribute Values::::");
			while (xr.ReadAttributeValue ())
				DumpReader (xr, true);
			Console.WriteLine (":::Attribute Values End");
		} else if (!attValue) {
			Console.WriteLine ("Attributes::::");
			Console.Write (xr.AttributeCount);
			if (xr.MoveToFirstAttribute ()) {
				do {
					DumpReader (xr, false);
				} while (xr.MoveToNextAttribute ());
				xr.MoveToElement ();
			}
			Console.WriteLine (":::Attributes End");
		}
	}
Example #2
0
 internal void ReadContentFrom(XmlReader r, LoadOptions o)
 {
     if ((o & (LoadOptions.SetBaseUri | LoadOptions.SetLineInfo)) == 0)
     {
         ReadContentFrom(r);
         return;
     }
     if (r.ReadState != ReadState.Interactive) throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive);
     XContainer c = this;
     XNode n = null;
     NamespaceCache eCache = new NamespaceCache();
     NamespaceCache aCache = new NamespaceCache();
     string baseUri = (o & LoadOptions.SetBaseUri) != 0 ? r.BaseURI : null;
     IXmlLineInfo li = (o & LoadOptions.SetLineInfo) != 0 ? r as IXmlLineInfo : null;
     do
     {
         string uri = r.BaseURI;
         switch (r.NodeType)
         {
             case XmlNodeType.Element:
                 {
                     XElement e = new XElement(eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                     if (baseUri != null && baseUri != uri)
                     {
                         e.SetBaseUri(uri);
                     }
                     if (li != null && li.HasLineInfo())
                     {
                         e.SetLineInfo(li.LineNumber, li.LinePosition);
                     }
                     if (r.MoveToFirstAttribute())
                     {
                         do
                         {
                             XAttribute a = new XAttribute(aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                             if (li != null && li.HasLineInfo())
                             {
                                 a.SetLineInfo(li.LineNumber, li.LinePosition);
                             }
                             e.AppendAttributeSkipNotify(a);
                         } while (r.MoveToNextAttribute());
                         r.MoveToElement();
                     }
                     c.AddNodeSkipNotify(e);
                     if (!r.IsEmptyElement)
                     {
                         c = e;
                         if (baseUri != null)
                         {
                             baseUri = uri;
                         }
                     }
                     break;
                 }
             case XmlNodeType.EndElement:
                 {
                     if (c.content == null)
                     {
                         c.content = string.Empty;
                     }
                     // Store the line info of the end element tag.
                     // Note that since we've got EndElement the current container must be an XElement
                     XElement e = c as XElement;
                     Debug.Assert(e != null, "EndElement received but the current container is not an element.");
                     if (e != null && li != null && li.HasLineInfo())
                     {
                         e.SetEndElementLineInfo(li.LineNumber, li.LinePosition);
                     }
                     if (c == this) return;
                     if (baseUri != null && c.HasBaseUri)
                     {
                         baseUri = c.parent.BaseUri;
                     }
                     c = c.parent;
                     break;
                 }
             case XmlNodeType.Text:
             case XmlNodeType.SignificantWhitespace:
             case XmlNodeType.Whitespace:
                 if ((baseUri != null && baseUri != uri) ||
                     (li != null && li.HasLineInfo()))
                 {
                     n = new XText(r.Value);
                 }
                 else
                 {
                     c.AddStringSkipNotify(r.Value);
                 }
                 break;
             case XmlNodeType.CDATA:
                 n = new XCData(r.Value);
                 break;
             case XmlNodeType.Comment:
                 n = new XComment(r.Value);
                 break;
             case XmlNodeType.ProcessingInstruction:
                 n = new XProcessingInstruction(r.Name, r.Value);
                 break;
             case XmlNodeType.DocumentType:
                 n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value);
                 break;
             case XmlNodeType.EntityReference:
                 if (!r.CanResolveEntity) throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                 r.ResolveEntity();
                 break;
             case XmlNodeType.EndEntity:
                 break;
             default:
                 throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
         }
         if (n != null)
         {
             if (baseUri != null && baseUri != uri)
             {
                 n.SetBaseUri(uri);
             }
             if (li != null && li.HasLineInfo())
             {
                 n.SetLineInfo(li.LineNumber, li.LinePosition);
             }
             c.AddNodeSkipNotify(n);
             n = null;
         }
     } while (r.Read());
 }
Example #3
0
 internal void ReadContentFrom(XmlReader r)
 {
     if (r.ReadState != ReadState.Interactive) throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive);
     XContainer c = this;
     NamespaceCache eCache = new NamespaceCache();
     NamespaceCache aCache = new NamespaceCache();
     do
     {
         switch (r.NodeType)
         {
             case XmlNodeType.Element:
                 XElement e = new XElement(eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                 if (r.MoveToFirstAttribute())
                 {
                     do
                     {
                         e.AppendAttributeSkipNotify(new XAttribute(aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value));
                     } while (r.MoveToNextAttribute());
                     r.MoveToElement();
                 }
                 c.AddNodeSkipNotify(e);
                 if (!r.IsEmptyElement)
                 {
                     c = e;
                 }
                 break;
             case XmlNodeType.EndElement:
                 if (c.content == null)
                 {
                     c.content = string.Empty;
                 }
                 if (c == this) return;
                 c = c.parent;
                 break;
             case XmlNodeType.Text:
             case XmlNodeType.SignificantWhitespace:
             case XmlNodeType.Whitespace:
                 c.AddStringSkipNotify(r.Value);
                 break;
             case XmlNodeType.CDATA:
                 c.AddNodeSkipNotify(new XCData(r.Value));
                 break;
             case XmlNodeType.Comment:
                 c.AddNodeSkipNotify(new XComment(r.Value));
                 break;
             case XmlNodeType.ProcessingInstruction:
                 c.AddNodeSkipNotify(new XProcessingInstruction(r.Name, r.Value));
                 break;
             case XmlNodeType.DocumentType:
                 c.AddNodeSkipNotify(new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value));
                 break;
             case XmlNodeType.EntityReference:
                 if (!r.CanResolveEntity) throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                 r.ResolveEntity();
                 break;
             case XmlNodeType.EndEntity:
                 break;
             default:
                 throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
         }
     } while (r.Read());
 }