iTextSharp.text.xml.XmlDomWriter.Write C# (CSharp) Method

Write() public method

public Write ( XmlNode node ) : void
node System.Xml.XmlNode
return void
        public void Write(XmlNode node) {
            
            // is there anything to do?
            if (node == null) {
                return;
            }
            
            XmlNodeType type = node.NodeType;
            switch (type) {
                case XmlNodeType.Document: {
                    XmlDocument document = (XmlDocument)node;
                    fXML11 = false; //"1.1".Equals(GetVersion(document));
                    if (!fCanonical) {
                        if (fXML11) {
                            fOut.WriteLine("<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
                        } else {
                            fOut.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                        }
                        fOut.Flush();
                        Write(document.DocumentType);
                    }
                    Write(document.DocumentElement);
                    break;
                }
                
                case XmlNodeType.DocumentType: {
                    XmlDocumentType doctype = (XmlDocumentType)node;
                    fOut.Write("<!DOCTYPE ");
                    fOut.Write(doctype.Name);
                    String publicId = doctype.PublicId;
                    String systemId = doctype.SystemId;
                    if (publicId != null) {
                        fOut.Write(" PUBLIC '");
                        fOut.Write(publicId);
                        fOut.Write("' '");
                        fOut.Write(systemId);
                        fOut.Write('\'');
                    } else if (systemId != null) {
                        fOut.Write(" SYSTEM '");
                        fOut.Write(systemId);
                        fOut.Write('\'');
                    }
                    String internalSubset = doctype.InternalSubset;
                    if (internalSubset != null) {
                        fOut.WriteLine(" [");
                        fOut.Write(internalSubset);
                        fOut.Write(']');
                    }
                    fOut.WriteLine('>');
                    break;
                }
                
                case XmlNodeType.Element: {
                    fOut.Write('<');
                    fOut.Write(node.Name);
                    XmlAttribute[] attrs = SortAttributes(node.Attributes);
                    for (int i = 0; i < attrs.Length; i++) {
                        XmlAttribute attr = attrs[i];
                        fOut.Write(' ');
                        fOut.Write(attr.Name);
                        fOut.Write("=\"");
                        NormalizeAndPrint(attr.Value, true);
                        fOut.Write('"');
                    }
                    fOut.Write('>');
                    fOut.Flush();
                    
                    XmlNode child = node.FirstChild;
                    while (child != null) {
                        Write(child);
                        child = child.NextSibling;
                    }
                    break;
                }
                
                case XmlNodeType.EntityReference: {
                    if (fCanonical) {
                        XmlNode child = node.FirstChild;
                        while (child != null) {
                            Write(child);
                            child = child.NextSibling;
                        }
                    } else {
                        fOut.Write('&');
                        fOut.Write(node.Name);
                        fOut.Write(';');
                        fOut.Flush();
                    }
                    break;
                }
                
                case XmlNodeType.CDATA: {
                    if (fCanonical) {
                        NormalizeAndPrint(node.Value, false);
                    } else {
                        fOut.Write("<![CDATA[");
                        fOut.Write(node.Value);
                        fOut.Write("]]>");
                    }
                    fOut.Flush();
                    break;
                }
                
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                case XmlNodeType.Text: {
                    NormalizeAndPrint(node.Value, false);
                    fOut.Flush();
                    break;
                }
                
                case XmlNodeType.ProcessingInstruction: {
                    fOut.Write("<?");
                    fOut.Write(node.Name);
                    String data = node.Value;
                    if (data != null && data.Length > 0) {
                        fOut.Write(' ');
                        fOut.Write(data);
                    }
                    fOut.Write("?>");
                    fOut.Flush();
                    break;
                }
                
                case XmlNodeType.Comment: {
                    if (!fCanonical) {
                        fOut.Write("<!--");
                        String comment = node.Value;
                        if (comment != null && comment.Length > 0) {
                            fOut.Write(comment);
                        }
                        fOut.Write("-->");
                        fOut.Flush();
                    }
                    break;
                }
            }
            
            if (type == XmlNodeType.Element) {
                fOut.Write("</");
                fOut.Write(node.Name);
                fOut.Write('>');
                fOut.Flush();
            }
            
        } // Write(Node)
        

Usage Example

コード例 #1
0
 /**
  * Writes the document to a byte array.
  */
 public byte[] SerializeDoc() {
     XmlDomWriter xw = new XmlDomWriter();
     MemoryStream fout = new MemoryStream();
     xw.SetOutput(fout, null);
     byte[] b = new UTF8Encoding(false).GetBytes(XmpWriter.XPACKET_PI_BEGIN);
     fout.Write(b, 0, b.Length);
     fout.Flush();
     XmlNodeList xmpmeta = domDocument.GetElementsByTagName("x:xmpmeta");
     xw.Write(xmpmeta[0]);
     fout.Flush();
     b = new UTF8Encoding(false).GetBytes(XmpWriter.EXTRASPACE);
     for (int i = 0; i < 20; i++) {
         fout.Write(b, 0, b.Length);
     }
     b = new UTF8Encoding(false).GetBytes(XmpWriter.XPACKET_PI_END_W);
     fout.Write(b, 0, b.Length);
     fout.Close();
     return fout.ToArray();
 }