本文整理汇总了C#中System.Xml.XmlWriter.WriteAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# XmlWriter.WriteAttributes方法的具体用法?C# XmlWriter.WriteAttributes怎么用?C# XmlWriter.WriteAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlWriter
的用法示例。
在下文中一共展示了XmlWriter.WriteAttributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteShallowNode
private static void WriteShallowNode(XmlReader reader, XmlWriter writer)
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteAttributes(reader, true);
if (reader.IsEmptyElement)
{
writer.WriteEndElement();
}
return;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
return;
case XmlNodeType.CDATA:
writer.WriteCData(reader.Value);
return;
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
return;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
return;
case XmlNodeType.DocumentType:
writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
return;
case XmlNodeType.Whitespace:
writer.WriteWhitespace(reader.Value);
return;
case XmlNodeType.SignificantWhitespace:
writer.WriteWhitespace(reader.Value);
return;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
return;
case XmlNodeType.XmlDeclaration:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
return;
}
throw new InvalidOperationException("Invalid node");
}
示例2: InvokeMethod
private void InvokeMethod(XmlWriter w, string methodName)
{
byte[] buffer = new byte[10];
switch (methodName)
{
case "WriteStartDocument":
w.WriteStartDocument();
break;
case "WriteStartElement":
w.WriteStartElement("root");
break;
case "WriteEndElement":
w.WriteEndElement();
break;
case "WriteStartAttribute":
w.WriteStartAttribute("attr");
break;
case "WriteEndAttribute":
w.WriteEndAttribute();
break;
case "WriteCData":
w.WriteCData("test");
break;
case "WriteComment":
w.WriteComment("test");
break;
case "WritePI":
w.WriteProcessingInstruction("name", "test");
break;
case "WriteEntityRef":
w.WriteEntityRef("e");
break;
case "WriteCharEntity":
w.WriteCharEntity('c');
break;
case "WriteSurrogateCharEntity":
w.WriteSurrogateCharEntity('\uDC00', '\uDBFF');
break;
case "WriteWhitespace":
w.WriteWhitespace(" ");
break;
case "WriteString":
w.WriteString("foo");
break;
case "WriteChars":
char[] charArray = new char[] { 'a', 'b', 'c', 'd' };
w.WriteChars(charArray, 0, 3);
break;
case "WriteRaw":
w.WriteRaw("<foo>bar</foo>");
break;
case "WriteBase64":
w.WriteBase64(buffer, 0, 9);
break;
case "WriteBinHex":
w.WriteBinHex(buffer, 0, 9);
break;
case "LookupPrefix":
string str = w.LookupPrefix("foo");
break;
case "WriteNmToken":
w.WriteNmToken("foo");
break;
case "WriteName":
w.WriteName("foo");
break;
case "WriteQualifiedName":
w.WriteQualifiedName("foo", "bar");
break;
case "WriteValue":
w.WriteValue(Int32.MaxValue);
break;
case "WriteAttributes":
XmlReader xr1 = ReaderHelper.Create(new StringReader("<root attr='test'/>"));
xr1.Read();
w.WriteAttributes(xr1, false);
break;
case "WriteNodeReader":
XmlReader xr2 = ReaderHelper.Create(new StringReader("<root/>"));
xr2.Read();
w.WriteNode(xr2, false);
break;
case "Flush":
w.Flush();
break;
default:
CError.Equals(false, "Unexpected param in testcase: {0}", methodName);
break;
}
}
示例3: WriteShallowNode
public static void WriteShallowNode(XmlReader reader, XmlWriter writer)
{
//Console.WriteLine("{0}: {1}", reader.NodeType, reader.Name);
switch(reader.NodeType) {
case XmlNodeType.Element:
writer.WriteStartElement(reader.Prefix , reader.LocalName, reader.NamespaceURI);
writer.WriteAttributes(reader, true);
if(reader.IsEmptyElement)
writer.WriteEndElement();
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
writer.WriteWhitespace(reader.Value);
break;
case XmlNodeType.CDATA:
writer.WriteCData(reader.Value);
break;
case XmlNodeType.EntityReference:
writer.WriteEntityRef(reader.Name);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.DocumentType:
writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
示例4: CopyUntilEndOfChanges
private static void CopyUntilEndOfChanges(XmlReader reader, XmlWriter writer)
{
Debug.Assert(writer != null);
Debug.Assert(reader != null);
reader.MoveToContent();
while (!(reader.EOF || reader.NodeType == XmlNodeType.EndElement && reader.Name == "Changes"))
{
if (reader.NodeType == XmlNodeType.Element)
{
writer.WriteStartElement(reader.Name);
writer.WriteAttributes(reader, false);
}
else if (reader.NodeType == XmlNodeType.Text)
{
writer.WriteString(reader.Value);
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
writer.WriteEndElement();
}
reader.Read();
}
}
示例5: WriteNodeAsync
// Writes the content (inner XML) of the current node into the provided XmlWriter.
private async Task WriteNodeAsync(XmlWriter xtw, bool defattr)
{
int d = this.NodeType == XmlNodeType.None ? -1 : this.Depth;
while (await this.ReadAsync().ConfigureAwait(false) && (d < this.Depth))
{
switch (this.NodeType)
{
case XmlNodeType.Element:
xtw.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceURI);
xtw.WriteAttributes(this, defattr);
if (this.IsEmptyElement)
{
xtw.WriteEndElement();
}
break;
case XmlNodeType.Text:
xtw.WriteString(await this.GetValueAsync().ConfigureAwait(false));
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
xtw.WriteWhitespace(await this.GetValueAsync().ConfigureAwait(false));
break;
case XmlNodeType.CDATA:
xtw.WriteCData(this.Value);
break;
case XmlNodeType.EntityReference:
xtw.WriteEntityRef(this.Name);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
xtw.WriteProcessingInstruction(this.Name, this.Value);
break;
case XmlNodeType.DocumentType:
xtw.WriteDocType(this.Name, this.GetAttribute("PUBLIC"), this.GetAttribute("SYSTEM"), this.Value);
break;
case XmlNodeType.Comment:
xtw.WriteComment(this.Value);
break;
case XmlNodeType.EndElement:
xtw.WriteFullEndElement();
break;
}
}
if (d == this.Depth && this.NodeType == XmlNodeType.EndElement)
{
await ReadAsync().ConfigureAwait(false);
}
}
示例6: PassNodeThrough
//copies from example
private void PassNodeThrough(XmlReader reader, XmlWriter writer)
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteAttributes(reader, true);
if (reader.IsEmptyElement)
{
writer.WriteEndElement();
}
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
writer.WriteWhitespace(reader.Value);
break;
case XmlNodeType.CDATA:
writer.WriteCData(reader.Value);
break;
case XmlNodeType.EntityReference:
writer.WriteEntityRef(reader.Name);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.DocumentType:
writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
示例7: CopyUntilElement
private void CopyUntilElement(XmlWriter writer, XmlReader reader, string elementName)
{
Debug.Assert(writer != null);
Debug.Assert(reader != null);
Debug.Assert(!string.IsNullOrEmpty(elementName));
if (reader.NodeType == XmlNodeType.None)
{
reader.Read();
}
while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement
&& (reader.NodeType != XmlNodeType.Element || LdmlNodeComparer.CompareElementNames(reader.Name, elementName) < 0))
{
// XmlWriter.WriteNode doesn't do anything if the node type is Attribute
if (reader.NodeType == XmlNodeType.Attribute)
{
writer.WriteAttributes(reader, false);
}
else
{
writer.WriteNode(reader, false);
}
}
}
示例8: LimitHtmlOnWordBoundary
private static void LimitHtmlOnWordBoundary(XmlWriter writer, XmlReader reader, int maxLength, out bool shouldWriteEllipses)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
if (writer == null)
{
throw new ArgumentNullException("writer");
}
int elementCount = 0;
int currentLength = 0;
shouldWriteEllipses = false;
int magicMinimumLength = Math.Min(5, (maxLength + 1) / 2);
int num = (reader.NodeType == XmlNodeType.None) ? -1 : reader.Depth;
do
{
bool done = false;
switch (reader.NodeType)
{
case XmlNodeType.Element:
elementCount++;
writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteAttributes(reader, false);
if (reader.IsEmptyElement)
{
elementCount--;
writer.WriteEndElement();
}
break;
case XmlNodeType.Text:
string value = reader.Value;
int strLen = value.Length;
if (currentLength + strLen > maxLength)
{
string almost = value.Substring(0, maxLength - currentLength + 1);
int lastSpace = almost.LastIndexOf(' ');
if (lastSpace < 0)
{
if (currentLength < magicMinimumLength)
{
value = value.Substring(0, maxLength - currentLength);
}
else
{
value = null;
}
}
else if (lastSpace + currentLength < magicMinimumLength)
{
value = value.Substring(0, maxLength - currentLength);
}
else
{
value = value.Substring(0, lastSpace);
}
shouldWriteEllipses = true;
done = true;
}
if (value != null)
{
writer.WriteString(value);
currentLength += value.Length;
}
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
writer.WriteString(reader.Value);
currentLength += reader.Value.Length;
break;
case XmlNodeType.EndElement:
elementCount--;
writer.WriteFullEndElement();
break;
case XmlNodeType.CDATA:
//writer.WriteCData(reader.Value);
break;
case XmlNodeType.EntityReference:
writer.WriteEntityRef(reader.Name);
currentLength++;
break;
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.XmlDeclaration:
//writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
//writer.WriteComment(reader.Value);
break;
//.........这里部分代码省略.........
示例9: StreamToInsertionPoint
private void StreamToInsertionPoint(XmlReader reader, XmlWriter writer)
{
if ( reader == null )
{
throw new ArgumentNullException("reader");
}
if ( writer == null )
{
throw new ArgumentNullException("writer");
}
while (reader.Read())
{
if(reader.LocalName == "notes" && reader.IsEmptyElement)
{
writer.WriteStartElement("notes");
writer.WriteAttributes(reader, false);
return;
}
if(reader.LocalName == "notes" && !reader.IsStartElement())
{
return;
}
StreamNode(reader, writer);
}
}
示例10: WriteShallowNode
// From http://blogs.msdn.com/b/mfussell/archive/2005/02/12/371546.aspx
static void WriteShallowNode( XmlReader reader, XmlWriter writer )
{
if ( reader == null )
{
throw new ArgumentNullException("reader");
}
if ( writer == null )
{
throw new ArgumentNullException("writer");
}
switch ( reader.NodeType )
{
case XmlNodeType.Element:
writer.WriteStartElement( reader.Prefix, reader.LocalName, reader.NamespaceURI );
writer.WriteAttributes( reader, true );
if ( reader.IsEmptyElement )
{
writer.WriteEndElement();
}
break;
case XmlNodeType.Text:
writer.WriteString( reader.Value );
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
writer.WriteWhitespace(reader.Value);
break;
case XmlNodeType.CDATA:
writer.WriteCData( reader.Value );
break;
case XmlNodeType.EntityReference:
writer.WriteEntityRef(reader.Name);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction( reader.Name, reader.Value );
break;
case XmlNodeType.DocumentType:
writer.WriteDocType( reader.Name, reader.GetAttribute( "PUBLIC" ), reader.GetAttribute( "SYSTEM" ), reader.Value );
break;
case XmlNodeType.Comment:
writer.WriteComment( reader.Value );
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
示例11: WriteTopic
/// <summary>
/// This is used to write out the body content of a topic to the main document part
/// </summary>
/// <param name="writer">The writer for the main document part</param>
/// <param name="reader">The reader for the topic body content</param>
/// <remarks>Using a reader prevents unnecessary namespaces from appearing on the body content elements
/// which happens if we convert the XElement content to a string and write it out as raw content.</remarks>
private static void WriteTopic(XmlWriter writer, XmlReader reader)
{
while(!reader.EOF)
{
reader.Read();
switch(reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteAttributes(reader, true);
if(reader.IsEmptyElement)
writer.WriteEndElement();
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
writer.WriteWhitespace(reader.Value);
break;
case XmlNodeType.CDATA:
writer.WriteCData(reader.Value);
break;
case XmlNodeType.EntityReference:
writer.WriteEntityRef(reader.Name);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
// Stop when the end of the body is reached
if(reader.LocalName == "body")
return;
writer.WriteFullEndElement();
break;
}
}
}
示例12: ProcessElement
private static void ProcessElement(XmlReader reader, XmlWriter writer)
{
if (reader.Name == "entry")
{
string guid = reader.GetAttribute("guid");
if (String.IsNullOrEmpty(guid))
{
guid = Guid.NewGuid().ToString();
writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
writer.WriteAttributes(reader, true);
writer.WriteAttributeString("guid", guid);
string s = reader.ReadInnerXml();//this seems to be enough to get the reader to the next element
writer.WriteRaw(s);
writer.WriteEndElement();
}
else
{
writer.WriteNode(reader, true);
}
}
else
{
WriteShallowNode(reader, writer);
}
}
示例13: CopyNode
protected void CopyNode(XmlReader reader, XmlWriter writer)
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
writer.WriteAttributes(reader, false);
if (reader.IsEmptyElement)
{
writer.WriteEndElement();
}
break;
case XmlNodeType.EndElement:
writer.WriteEndElement();
break;
case XmlNodeType.Text:
writer.WriteValue(reader.Value);
break;
}
}
示例14: WriteNode
// Writes the content (inner XML) of the current node into the provided XmlWriter.
private void WriteNode(XmlWriter xtw, bool defattr) {
#if !SILVERLIGHT
Debug.Assert(xtw is XmlTextWriter);
#endif
int d = this.NodeType == XmlNodeType.None ? -1 : this.Depth;
while (this.Read() && (d < this.Depth)) {
switch (this.NodeType) {
case XmlNodeType.Element:
xtw.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceURI);
#if !SILVERLIGHT // Removing dependency on XmlTextWriter
((XmlTextWriter)xtw).QuoteChar = this.QuoteChar;
#endif
xtw.WriteAttributes(this, defattr);
if (this.IsEmptyElement) {
xtw.WriteEndElement();
}
break;
case XmlNodeType.Text:
xtw.WriteString(this.Value);
break;
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
xtw.WriteWhitespace(this.Value);
break;
case XmlNodeType.CDATA:
xtw.WriteCData(this.Value);
break;
case XmlNodeType.EntityReference:
xtw.WriteEntityRef(this.Name);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
xtw.WriteProcessingInstruction(this.Name, this.Value);
break;
case XmlNodeType.DocumentType:
xtw.WriteDocType(this.Name, this.GetAttribute("PUBLIC"), this.GetAttribute("SYSTEM"), this.Value);
break;
case XmlNodeType.Comment:
xtw.WriteComment(this.Value);
break;
case XmlNodeType.EndElement:
xtw.WriteFullEndElement();
break;
}
}
if (d == this.Depth && this.NodeType == XmlNodeType.EndElement) {
Read();
}
}
示例15: CopyToEndElement
private void CopyToEndElement(XmlWriter writer, XmlReader reader)
{
Debug.Assert(writer != null);
Debug.Assert(reader != null);
while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement)
{
// XmlWriter.WriteNode doesn't do anything if the node type is Attribute
if (reader.NodeType == XmlNodeType.Attribute)
{
writer.WriteAttributes(reader, false);
}
else
{
writer.WriteNode(reader, false);
}
}
// either read the end element or no-op if EOF
reader.Read();
}