示例#1
0
        private void button2_Click_1(object sender, EventArgs e)
        {
            XmlTextWriter writer = null;
            StringWriter  w      = new StringWriter();

            writer = new XmlTextWriter(w)
            {
                Formatting  = Formatting.Indented,
                Indentation = 4
            };
            writer.WriteComment("sample XML fragment");
            writer.WriteStartElement("book");
            writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
            writer.WriteAttributeString("genre", "novel");
            writer.WriteStartElement("title");
            writer.WriteString("The Handmaid's Tale");
            writer.WriteEndElement();
            writer.WriteElementString("price", "19.95");
            string prefix = writer.LookupPrefix("urn:samples");

            writer.WriteStartElement(prefix, "ISBN", "urn:samples");
            writer.WriteString("1-861003-78");
            writer.WriteEndElement();
            writer.WriteElementString("style", "urn:samples", "hardcover");
            writer.WriteEndElement();
            MessageBox.Show(w.ToString());
        }
示例#2
0
        public Element GetXmlElement(Type type)
        {
            if (type == typeof(string))
            {
                return(elementString);
            }
            Element element = (Element)typeToXmlNodeTable[type.AssemblyQualifiedName];

            if (element == null)
            {
                element = GetXmlElement(type.FullName, type.Assembly.FullName);
                //					if(_assemblyFormat == FormatterAssemblyStyle.Full)
                //						element = this[type.FullName, type.Assembly.FullName];
                //					else
                //						element = this[type.FullName, type.Assembly.GetName().Name];
            }
            else if (_xmlWriter != null)
            {
                element = new Element((element.Prefix == null) ? _xmlWriter.LookupPrefix(element.NamespaceURI) : element.Prefix, element.LocalName, element.NamespaceURI);
            }
            if (element == null)
            {
                throw new SerializationException("Oooops");
            }
            return(element);
        }
示例#3
0
    public static void Main()
    {
        XmlTextWriter writer = new XmlTextWriter(filename, null);

        //Use indenting for readability.
        writer.Formatting = Formatting.Indented;

        writer.WriteComment("sample XML fragment");

        //Write an element (this one is the root).
        writer.WriteStartElement("bookstore");

        //Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

        writer.WriteStartElement("book");

        //Lookup the prefix and then write the ISBN attribute.
        string prefix = writer.LookupPrefix("urn:samples");

        writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
        writer.WriteString("1-861003-78");
        writer.WriteEndAttribute();

        //Write the title.
        writer.WriteStartElement("title");
        writer.WriteString("The Handmaid's Tale");
        writer.WriteEndElement();

        //Write the price.
        writer.WriteElementString("price", "19.95");

        //Write the style element.
        writer.WriteStartElement(prefix, "style", "urn:samples");
        writer.WriteString("hardcover");
        writer.WriteEndElement();

        //Write the end tag for the book element.
        writer.WriteEndElement();

        //Write the close tag for the root element.
        writer.WriteEndElement();

        //Write the XML to file and close the writer.
        writer.Flush();
        writer.Close();

        //Read the file back in and parse to ensure well formed XML.
        XmlDocument doc = new XmlDocument();

        //Preserve white space for readability.
        doc.PreserveWhitespace = true;
        //Load the file
        doc.Load(filename);

        //Write the XML content to the console.
        Console.Write(doc.InnerXml);
    }
示例#4
0
        public IValue LookupPrefix(string uri)
        {
            string prefix = _writer.LookupPrefix(uri);

            if (prefix == null)
            {
                return(ValueFactory.Create());
            }
            return(ValueFactory.Create(prefix));
        }
示例#5
0
        string GetQualifiedNameString(XmlTextWriter xtw, XmlQualifiedName qname)
        {
            string pref = xtw.LookupPrefix(qname.Namespace);

            if (pref != null)
            {
                return(pref + ":" + qname.Name);
            }

            xtw.WriteAttributeString("xmlns", "q1", null, qname.Namespace);
            return("q1:" + qname.Name);
        }
示例#6
0
        public string GetNamespacePrefix(Element xmlType)
        {
            string prefix = _xmlWriter.LookupPrefix(xmlType.NamespaceURI);

            if (prefix == null || prefix == string.Empty)
            {
                _xmlWriter.WriteAttributeString(
                    "xmlns",
                    xmlType.Prefix,
                    "http://www.w3.org/2000/xmlns/",
                    xmlType.NamespaceURI);
                return(xmlType.Prefix);
            }
            return(prefix);
        }
示例#7
0
        void WriteComplexTypeSample(XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName, int id)
        {
            string ns = rootName.Namespace;

            if (rootName.Name.IndexOf("[]") != -1)
            {
                rootName = arrayType;
            }

            if (currentUse == SoapBindingUse.Encoded)
            {
                string pref = xtw.LookupPrefix(rootName.Namespace);
                if (pref == null)
                {
                    pref = "q1";
                }
                xtw.WriteStartElement(pref, rootName.Name, rootName.Namespace);
                ns = "";
            }
            else
            {
                xtw.WriteStartElement(rootName.Name, rootName.Namespace);
            }

            if (id != -1)
            {
                xtw.WriteAttributeString("id", "id" + id);
                if (rootName != arrayType)
                {
                    xtw.WriteAttributeString("type", XmlSchema.InstanceNamespace, GetQualifiedNameString(xtw, rootName));
                }
            }

            WriteComplexTypeAttributes(xtw, stype);
            WriteComplexTypeElements(xtw, ns, stype);

            xtw.WriteEndElement();
        }
 public void LookupPrefix(string uri)
 {
     _writer.LookupPrefix(uri);
 }
示例#9
0
        // return:
        //		0	成功
        //		-1	出错
        public int WriteRecord(
            string[] saField,
            out string strError)
        {
            string strFieldName = null;

            // int nRet;

            strError = "";

            // long lStart = writer.BaseStream.Position;
            // Debug.Assert(writer.BaseStream.CanSeek == true, "writer.BaseStream.CanSeek != true");

            //根据WriteMarcPrefix的值,确定是否对元素record加命名空间
            if (WriteMarcPrefix == false)
            {
                _writer.WriteStartElement("record");
            }
            else
            {
                _writer.WriteStartElement(MarcPrefix,
                                          "record", MarcNameSpaceUri);
            }

            if (String.IsNullOrEmpty(_writer.LookupPrefix("dprms")) == true)
            {
                // dprms名字空间 2010/11/15
                _writer.WriteAttributeString("xmlns", "dprms", null, DpNs.dprms);
            }

            //循环,写头标区及每个子段
            for (int i = 0; i < saField.Length; i++)
            {
                string strLine    = saField[i];
                string strInd1    = null;
                string strInd2    = null;
                string strContent = null;

                // 头标区
                if (i == 0)
                {
                    // 多截少添
                    if (strLine.Length > 24)
                    {
                        strLine = strLine.Substring(0, 24);
                    }
                    else
                    {
                        while (strLine.Length < 24)
                        {
                            strLine += " ";
                        }
                    }

                    if (WriteMarcPrefix == false)
                    {
                        _writer.WriteElementString("leader", strLine);
                    }
                    else
                    {
                        _writer.WriteElementString("leader", MarcNameSpaceUri, strLine);
                    }

                    continue;
                }

                Debug.Assert(strLine != null, "");

                // 不合法的字段,不算数
                if (strLine.Length < 3)
                {
                    continue;
                }

                strFieldName = strLine.Substring(0, 3);
                if (strLine.Length >= 3)
                {
                    strContent = strLine.Substring(3);
                }
                else
                {
                    strContent = "";
                }

                // control field  001-009 没有子字段

                /*
                 *              if ( (String.Compare(strFieldName, "001") >= 0
                 *                      && String.Compare(strFieldName, "009") <= 0 )
                 || String.Compare(strFieldName, "-01") == 0)
                 * */
                if (MarcUtil.IsControlFieldName(strFieldName) == true)
                {
                    if (WriteMarcPrefix == false)
                    {
                        _writer.WriteStartElement("controlfield");
                    }
                    else
                    {
                        _writer.WriteStartElement(MarcPrefix,
                                                  "controlfield", MarcNameSpaceUri);
                    }

                    _writer.WriteAttributeString("tag", strFieldName);

                    _writer.WriteString(strContent);
                    _writer.WriteEndElement();
                    continue;
                }

                if (strLine.Length == 3)
                {
                    strInd1    = " ";
                    strInd2    = " ";
                    strContent = "";
                }
                //字段长度等于4的情况,这样做是为了防止越界
                else if (strLine.Length == 4)
                {
                    strInd1    = strContent[0].ToString();
                    strInd2    = " ";
                    strContent = "";
                }
                else
                {
                    strInd1    = strContent[0].ToString();
                    strInd2    = strContent[1].ToString();
                    strContent = strContent.Substring(2);
                }

                // 普通字段
                if (WriteMarcPrefix == false)
                {
                    _writer.WriteStartElement("datafield");
                }
                else
                {
                    _writer.WriteStartElement(MarcPrefix,
                                              "datafield", MarcNameSpaceUri);
                }

                _writer.WriteAttributeString("tag", strFieldName);
                _writer.WriteAttributeString("ind1", strInd1);
                _writer.WriteAttributeString("ind2", strInd2);

                // 得到子字段数组

                string[] aSubfield = strContent.Split(new char[] { (char)31 });
                if (aSubfield == null)
                {
                    // 不太可能发生
                    continue;
                }

                // 循环写子字段
                for (int j = 0; j < aSubfield.Length; j++)
                {
                    string strValue           = aSubfield[j];
                    string strSubfieldName    = "";
                    string strSubfieldContent = "";

                    if (j == 0)
                    {
                        // 第一个空字符串要被跳过。其余的,将来返还时会用来产生一个单独的 31 字符
                        if (string.IsNullOrEmpty(aSubfield[0]) == true)
                        {
                            continue;
                        }
                        strSubfieldName    = null; // 表示后面不要创建code属性
                        strSubfieldContent = strValue;
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(strValue) == false)
                        {
                            strSubfieldName = strValue.Substring(0, 1);
                        }
                        if (string.IsNullOrEmpty(strValue) == false)
                        {
                            strSubfieldContent = strValue.Substring(1);
                        }
                    }

                    if (WriteMarcPrefix == false)
                    {
                        _writer.WriteStartElement("subfield");
                    }
                    else
                    {
                        _writer.WriteStartElement(MarcPrefix,
                                                  "subfield", MarcNameSpaceUri);
                    }

                    if (strSubfieldName != null)
                    {
                        _writer.WriteAttributeString("code", strSubfieldName);
                    }
                    _writer.WriteString(strSubfieldContent); //注意这里是否有越界的危险
                    _writer.WriteEndElement();
                }

                _writer.WriteEndElement();
            }

            _writer.WriteEndElement();
            return(0);
        }
示例#10
0
 public override string LookupPrefix(string ns)
 {
     return(xmlWriter.LookupPrefix(ns));
 }
示例#11
0
 string GetQualifiedName(XmlTextWriter tw, string namspace, string localName)
 {
     return(tw.LookupPrefix(namspace) + ":" + localName);
 }
示例#12
0
        public void ExportTypes(ServiceType[] servicetypes, SdlType sdltype, XmlTextWriter tw)
        {
            if (sdltype == SdlType.Sdl)                 // Obsolete, we don't support this
            {
                throw new NotSupportedException();
            }

            if (servicetypes.Length == 0)
            {
                return;
            }
            Type maint = servicetypes [0].ObjectType;

            Hashtable dataTypes = new Hashtable();
            ArrayList services  = new ArrayList();

            FindTypes(servicetypes, dataTypes, services);

            if (services.Count > 0)
            {
                maint = ((ServiceType)services[0]).ObjectType;
            }

            string serviceNs = GetXmlNamespace(maint, null);

            tw.Formatting = Formatting.Indented;
            tw.WriteStartElement("definitions", MetaData.WsdlNamespace);
            tw.WriteAttributeString("name", maint.Name);
            tw.WriteAttributeString("targetNamespace", serviceNs);
            tw.WriteAttributeString("xmlns", MetaData.XmlnsNamespace, MetaData.WsdlNamespace);
            tw.WriteAttributeString("xmlns", "tns", MetaData.XmlnsNamespace, serviceNs);
            tw.WriteAttributeString("xmlns", "xsd", MetaData.XmlnsNamespace, MetaData.SchemaNamespace);
            tw.WriteAttributeString("xmlns", "xsi", MetaData.XmlnsNamespace, MetaData.SchemaInstanceNamespace);
            tw.WriteAttributeString("xmlns", "suds", MetaData.XmlnsNamespace, MetaData.SudsNamespace);
            tw.WriteAttributeString("xmlns", "wsdl", MetaData.XmlnsNamespace, MetaData.WsdlNamespace);
            tw.WriteAttributeString("xmlns", "soapenc", MetaData.XmlnsNamespace, MetaData.SoapEncodingNamespace);
            tw.WriteAttributeString("xmlns", "soap", MetaData.XmlnsNamespace, MetaData.SoapNamespace);

            int nums = 0;

            foreach (DictionaryEntry entry in dataTypes)
            {
                string ns = (string)entry.Key;
                if (tw.LookupPrefix(ns) != null)
                {
                    continue;
                }
                tw.WriteAttributeString("xmlns", "ns" + nums, MetaData.XmlnsNamespace, ns);
                nums++;
            }

            // Schema

            if (dataTypes.Count > 0)
            {
                tw.WriteStartElement("types", MetaData.WsdlNamespace);
                foreach (DictionaryEntry entry in dataTypes)
                {
                    SchemaInfo sinfo = (SchemaInfo)entry.Value;
                    if (sinfo == null || sinfo.Types.Count == 0)
                    {
                        continue;
                    }

                    tw.WriteStartElement("s", "schema", MetaData.SchemaNamespace);
                    tw.WriteAttributeString("targetNamespace", (string)entry.Key);
                    tw.WriteAttributeString("elementFormDefault", "unqualified");
                    tw.WriteAttributeString("attributeFormDefault", "unqualified");

                    foreach (string ns in sinfo.Imports)
                    {
                        if (ns == (string)entry.Key)
                        {
                            continue;
                        }
                        tw.WriteStartElement("import", MetaData.SchemaNamespace);
                        tw.WriteAttributeString("namespace", ns);
                        tw.WriteEndElement();
                    }

                    foreach (Type type in sinfo.Types)
                    {
                        WriteDataTypeSchema(tw, type);
                    }

                    tw.WriteEndElement();
                }
                tw.WriteEndElement();
            }

            // Bindings

/*			foreach (ServiceType st in servicetypes)
 *                              WriteServiceBinding (tw, st);
 */
            foreach (ServiceType st in services)
            {
                WriteServiceBinding(tw, st, dataTypes);
            }

            // Service element

            tw.WriteStartElement("service", MetaData.WsdlNamespace);
            if (services.Count > 0)
            {
                tw.WriteAttributeString("name", GetServiceName(maint));
            }
            else
            {
                tw.WriteAttributeString("name", "Service");
            }

            foreach (ServiceType st in services)
            {
                WriteServiceType(tw, st);
            }
            tw.WriteEndElement();

            // Closing

            tw.WriteEndElement();
            tw.Flush();
        }
        private static void WriteNode(XmlTextWriter writer, TreeNode node)
        {
            SchemaParser.NodeData data = null;
            if (node.Tag != null)
            {
                data = node.Tag as SchemaParser.NodeData;
            }

            if (data == null)
            {
                return;
            }

            if (data.baseObj is XmlSchemaElement ||
                data.baseObj is XmlSchemaComplexType ||
                data.baseObj is XmlSchemaSimpleType
                )
            {
                //if (pref != null)  pref + ":" + node.Text;
                string name = node.Text;
                if (name.LastIndexOf(":") != -1)
                {
                    name = name.Substring(name.LastIndexOf(":") + 1, name.Length - name.LastIndexOf(":") - 1);
                }

                if (data.Namespace != string.Empty)
                {
                    ++prefIndex;
                    string pref = writer.LookupPrefix(data.Namespace);

                    if (pref == null)
                    {
                        pref = "m" + prefIndex;
                    }

                    writer.WriteStartElement(pref, name, data.Namespace);
                }
                else
                {
                    writer.WriteStartElement(name);
                }

                bool ct = false;
                if (node.Nodes.Count > 0)
                {
                    foreach (TreeNode child in node.Nodes)
                    {
                        if (child.Tag is SchemaParser.NodeData)
                        {
                            if (((SchemaParser.NodeData)child.Tag).baseObj is XmlSchemaAttribute)
                            {
                                writer.WriteAttributeString(child.Text, ((SchemaParser.NodeData)child.Tag).Type);
                            }
                        }
                    }

                    foreach (TreeNode child in node.Nodes)
                    {
                        if (child.Tag is SchemaParser.NodeData)
                        {
                            if (!(((SchemaParser.NodeData)child.Tag).baseObj is XmlSchemaAttribute))
                            {
                                ct = true;
                                WriteNode(writer, child);
                            }
                        }
                    }
                }
                ;

                if (!ct)
                {
                    writer.WriteString(data.Type);
                }

                writer.WriteEndElement();
            }
        }
        private void ProcessFile_Click(object sender, RoutedEventArgs e)
        {
            // choose a file to process
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter           = "Word files (*.doc)|*.doc;*.docx";
            openFileDialog1.FilterIndex      = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string doc_name;

                // init global data
                font_name_tbl  = new ArrayList();
                font_char_cnt  = new int[MAX_NUM_FONTS];
                font_glyph_cnt = new int[MAX_NUM_FONTS];

                // process the file
                doc_name = openFileDialog1.FileName;
                AnalyzeDocument(doc_name);

                // output the result in XML format

                int indx, num_fonts;

                XmlTextWriter writer = new XmlTextWriter("c:\\fontinfo.xml", null);
                //Use indenting for readability.
                writer.Formatting = Formatting.Indented;

                writer.WriteComment("FontAnalysis");

                //Write an element (this one is the root).
                writer.WriteStartElement("FontAnalysis");

                //Write the namespace declaration.
                writer.WriteAttributeString("xmlns", "font", null, "urn:FontAnalysis");

                writer.WriteStartElement("file");

                string prefix = writer.LookupPrefix("urn:FontAnalysis");
                writer.WriteStartAttribute(prefix, "file_name", "urn:FontAnalysis");
                writer.WriteString(doc_name);
                writer.WriteEndAttribute();

                num_fonts = font_name_tbl.Count;

                //display information for each font in document
                for (indx = 0; indx < num_fonts; indx++)
                {
                    //Write the font.
                    writer.WriteStartElement("font");
                    writer.WriteStartAttribute(prefix, "name", "urn:FontAnalysis");
                    writer.WriteString((string)font_name_tbl[indx]);
                    writer.WriteEndAttribute();

                    //Write the char count.
                    writer.WriteStartElement("char");
                    writer.WriteStartElement("count");
                    writer.WriteString(font_char_cnt[indx].ToString());
                    writer.WriteEndElement();
                    writer.WriteEndElement();

                    //Write the glyph count.
                    writer.WriteStartElement("glyph");
                    writer.WriteStartElement("count");
                    writer.WriteString(font_glyph_cnt[indx].ToString());
                    writer.WriteEndElement();
                    writer.WriteEndElement();

                    //Write the end tag for the font element.
                    writer.WriteEndElement();
                }

                //Write the end tag for the file element.
                writer.WriteEndElement();

                //Write the end tag for the FontAnalysis element.
                writer.WriteEndElement();

                //Write the XML to file and close the writer.
                writer.Flush();
                writer.Close();
            }
        }
示例#15
0
        private void BulkAnalyze_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            object oMissing = System.Reflection.Missing.Value;

            DialogResult result;

            // choose a directory
            result = folderBrowserDialog1.ShowDialog();
            if (true) //result )
            {
                selected_folder = folderBrowserDialog1.SelectedPath;

                if (selected_folder != "")
                {
                    string[] files = Directory.GetFiles(selected_folder, "*.docx");

                    XmlTextWriter writer = new XmlTextWriter("c:\\fontinfo.xml", null);
                    //Use indenting for readability.
                    writer.Formatting = Formatting.Indented;

                    writer.WriteComment("FontAnalysis");

                    //Write an element (this one is the root).
                    writer.WriteStartElement("FontAnalysis");
                    //Write the namespace declaration.
                    writer.WriteAttributeString("xmlns", "font", null, "urn:FontAnalysis");

                    foreach (string file in files)
                    {
                        // init global data
                        font_name_tbl      = new ArrayList();
                        font_char_cnt_tbl  = new int[MAX_NUM_FONTS];
                        font_glyph_cnt_tbl = new int[MAX_NUM_FONTS];

                        // for each doc file in the directory
                        // process the file and output the result
                        AnalyzeDocument(file);

                        // output the result in XML format

                        int indx, num_fonts;

                        writer.WriteStartElement("file");

                        string prefix = writer.LookupPrefix("urn:FontAnalysis");
                        writer.WriteStartAttribute(prefix, "file_name", "urn:FontAnalysis");
                        writer.WriteString(file);
                        writer.WriteEndAttribute();

                        num_fonts = font_name_tbl.Count;

                        //display information for each font in document
                        for (indx = 0; indx < num_fonts; indx++)
                        {
                            //Write the font.
                            writer.WriteStartElement("font");
                            writer.WriteStartAttribute(prefix, "name", "urn:FontAnalysis");
                            writer.WriteString((string)font_name_tbl[indx]);
                            writer.WriteEndAttribute();

                            //Write the char count.
                            writer.WriteStartElement("char");
                            writer.WriteStartElement("count");
                            writer.WriteString(font_char_cnt_tbl[indx].ToString());
                            writer.WriteEndElement();
                            writer.WriteEndElement();

                            //Write the glyph count.
                            writer.WriteStartElement("glyph");
                            writer.WriteStartElement("count");
                            writer.WriteString(font_glyph_cnt_tbl[indx].ToString());
                            writer.WriteEndElement();
                            writer.WriteEndElement();

                            //Write the end tag for the font element.
                            writer.WriteEndElement();
                        }

                        //Write the end tag for the file element.
                        writer.WriteEndElement();
                    }

                    //Write the end tag for the FontAnalysis element.
                    writer.WriteEndElement();

                    //Write the XML to file and close the writer.
                    writer.Flush();
                    writer.Close();
                }
            }
            // end the Wrrd App
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        }