当前位置: 首页>>代码示例>>C#>>正文


C# XmlTextWriter.WriteAttributes方法代码示例

本文整理汇总了C#中System.Xml.XmlTextWriter.WriteAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# XmlTextWriter.WriteAttributes方法的具体用法?C# XmlTextWriter.WriteAttributes怎么用?C# XmlTextWriter.WriteAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Xml.XmlTextWriter的用法示例。


在下文中一共展示了XmlTextWriter.WriteAttributes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConvertHtmlToXHtml

        /// <summary>
        /// Converts the specified html into XHTML compliant text.
        /// </summary>
        /// <param name="reader">sgml reader.</param>
        /// <param name="html">html to convert.</param>
        /// <param name="converter">The converter.</param>
        /// <returns></returns>
        /// ///
        private static string ConvertHtmlToXHtml(SgmlReader reader, string html, Converter<string, string> converter)
        {
            reader.DocType = "html";
            reader.WhitespaceHandling = WhitespaceHandling.All;
            // Hack to fix SF bug #1678030
            html = RemoveNewLineBeforeCDATA(html);
            reader.InputStream = new StringReader("<html>" + html + "</html>");
            reader.CaseFolding = CaseFolding.ToLower;
            StringWriter writer = new StringWriter();
            XmlWriter xmlWriter = null;
            try
            {
                xmlWriter = new XmlTextWriter(writer);

                bool insideAnchor = false;
                bool skipRead = false;
                while ((skipRead || reader.Read()) && !reader.EOF)
                {
                    skipRead = false;
                    switch(reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            //Special case for anchor tags for the time being.
                            //We need some way to communicate which elements the current node is nested within
                            if (reader.IsEmptyElement)
                            {
                                xmlWriter.WriteStartElement(reader.LocalName);
                                xmlWriter.WriteAttributes(reader, true);
                                if (reader.LocalName == "a" || reader.LocalName == "script")
                                    xmlWriter.WriteFullEndElement();
                                else
                                    xmlWriter.WriteEndElement();
                            }
                            else
                            {
                                if (reader.LocalName == "a")
                                    insideAnchor = true;
                                xmlWriter.WriteStartElement(reader.LocalName);
                                xmlWriter.WriteAttributes(reader, true);
                            }
                            break;

                        case XmlNodeType.Text:
                            string text = reader.Value;

                            if (converter != null && !insideAnchor)
                                xmlWriter.WriteRaw(converter(text));
                            else
                                xmlWriter.WriteString(text);
                            break;

                        case XmlNodeType.EndElement:
                            if (reader.LocalName == "a")
                                insideAnchor = false;

                            if (reader.LocalName == "a" || reader.LocalName == "script")
                                xmlWriter.WriteFullEndElement();
                            else
                                xmlWriter.WriteEndElement();
                            break;

                        default:
                            xmlWriter.WriteNode(reader, true);
                            skipRead = true;
                            break;
                    }
                }
            }
            finally
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Close();
                }
            }

            string xml = writer.ToString();
            return xml.Substring("<html>".Length, xml.Length - "<html></html>".Length);
        }
开发者ID:ayende,项目名称:Subtext,代码行数:87,代码来源:HtmlHelper.cs

示例2: PreprocessListItems

        private XmlTextReader PreprocessListItems(XmlTextReader xReader, PipelineContext context)
        {
            TextWriter sWriter = new StringWriter();
            XmlTextWriter xWriter = new XmlTextWriter(sWriter);
            string attrName = "metadataFieldValue";  // Must be same as extension config,  'selector="@metadataFieldValue"'
            string attrValue = "fieldValue";  // default here to confirm GUI DataExtender is working - will update value below

            xReader.MoveToContent();

            while (!xReader.EOF)
            {
                switch (xReader.NodeType)
                {
                    case XmlNodeType.Element:
                        xWriter.WriteStartElement(xReader.Prefix, xReader.LocalName, xReader.NamespaceURI);

                        // add all list attributes back  -- always START with this to NOT break the GUI
                        xWriter.WriteAttributes(xReader, false);

                        try
                        {
                            // add my custom attribute
                            if (IsValidItem(xReader))
                            {
                                string id = xReader.GetAttribute("ID");  // URI
                                TcmUri uri = new TcmUri(id);

                                if (uri.ItemType == ItemType.Component)
                                {
                                    // Do Work Here....
                                    attrValue = "your new value";
                                }

                                // add new metadata field attribute
                                xWriter.WriteAttributeString(attrName, attrValue);
                                xReader.MoveToElement();
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError("EXCEPTION " + ex.Message + ex.ToString() + ex.StackTrace);
                        }

                        if (xReader.IsEmptyElement)
                        {
                            xWriter.WriteEndElement();
                        }
                        break;

                    case XmlNodeType.EndElement:
                        xWriter.WriteEndElement();
                        break;
                    case XmlNodeType.CDATA:
                        // Copy CDATA node  <![CDATA[]]>
                        xWriter.WriteCData(xReader.Value);
                        break;
                    case XmlNodeType.Comment:
                        // Copy comment node <!-- -->
                        xWriter.WriteComment(xReader.Value);
                        break;
                    case XmlNodeType.DocumentType:
                        // Copy XML documenttype
                        xWriter.WriteDocType(xReader.Name, null, null, null);
                        break;
                    case XmlNodeType.EntityReference:
                        xWriter.WriteEntityRef(xReader.Name);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        xWriter.WriteProcessingInstruction(xReader.Name, xReader.Value);
                        break;
                    case XmlNodeType.SignificantWhitespace:
                        xWriter.WriteWhitespace(xReader.Value);
                        break;
                    case XmlNodeType.Text:
                        xWriter.WriteString(xReader.Value);
                        break;
                    case XmlNodeType.Whitespace:
                        xWriter.WriteWhitespace(xReader.Value);
                        break;
                }
                xReader.Read();
            };

            xWriter.Flush();

            xReader = new XmlTextReader(new StringReader(sWriter.ToString()));
            xReader.MoveToContent();

            //-> Write XML of tcm:Item out...
            //   This is where the attribute in the config file is matched -> selector="@metadataFieldValue"
            // Trace.Write(sWriter.ToString() + Environment.NewLine);
            return xReader;
        }
开发者ID:rcurlette,项目名称:DataExtenderMetadataCol,代码行数:93,代码来源:Example.cs

示例3: PreprocessListItems

        /// <summary>
        /// Idea here is to re-create the XmlTextReader Node and this accounts for 50% of the code.
        /// Original code borrowed from http://www.sdltridionworld.com/community/2011_extensions/parentchangenotifier.aspx
        /// Thanks for the work from Serguei Martchenko - would not be possible without his example!
        /// </summary>
        /// <param name="xReader"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private XmlTextReader PreprocessListItems(XmlTextReader xReader, PipelineContext context)
        {
            TextWriter sWriter = new StringWriter();
            XmlTextWriter xWriter = new XmlTextWriter(sWriter);
            string attrName = "metadataFieldValue";
            string attrValue = "";  // set this to 'fieldValue', for example, to debug and prove it is working

            xReader.MoveToContent();

            using(var tdse = new TDSEWrapper())
            {
                while (!xReader.EOF)
                {
                    switch (xReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            xWriter.WriteStartElement(xReader.Prefix, xReader.LocalName, xReader.NamespaceURI);

                            // add all attributes back  -- always START with this to NOT break the GUI
                            xWriter.WriteAttributes(xReader, false);

                            try
                            {
                                // add my custom attribute
                                if (IsValidItem(xReader))
                                {
                                    string id = xReader.GetAttribute("ID");  // URI
                                    TcmUri uri = new TcmUri(id);

                                    if (uri.ItemType == ItemType.Component)
                                    {
                                        // Get Metadata Value
                                        // 0 seconds with no processing
                                        // 12 seconds for 250 Components with TOM API, Component.MetadataFields...
                                        // 12 seconds for 250 Components with Core Service
                                        // 2 seconds for 250 Components with TOM API and GetXML  (FASTEST!)

                                        // Core Service
                                        //attrValue = GetMetadataValue(fieldName, id);

                                        // TDSE...Fastest with GetXML
                                        Component comp = tdse.TDSE.GetObject(id, Tridion.ContentManager.Interop.TDSDefines.EnumOpenMode.OpenModeView) as Component;
                                        attrValue = GetMetadataValue(comp, "article_number");
                                    }

                                    // add new metadata field attribute
                                    xWriter.WriteAttributeString(attrName, attrValue);
                                    xReader.MoveToElement();
                                }
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("EXCEPTION " + ex.Message + ex.ToString() + ex.StackTrace);
                            }

                            if (xReader.IsEmptyElement)
                            {
                                xWriter.WriteEndElement();
                            }
                            break;

                        case XmlNodeType.EndElement:
                            xWriter.WriteEndElement();
                            break;
                        case XmlNodeType.CDATA:
                            // Copy CDATA node  <![CDATA[]]>
                            xWriter.WriteCData(xReader.Value);
                            break;
                        case XmlNodeType.Comment:
                            // Copy comment node <!-- -->
                            xWriter.WriteComment(xReader.Value);
                            break;
                        case XmlNodeType.DocumentType:
                            // Copy XML documenttype
                            xWriter.WriteDocType(xReader.Name, null, null, null);
                            break;
                        case XmlNodeType.EntityReference:
                            xWriter.WriteEntityRef(xReader.Name);
                            break;
                        case XmlNodeType.ProcessingInstruction:
                            xWriter.WriteProcessingInstruction(xReader.Name, xReader.Value);
                            break;
                        case XmlNodeType.SignificantWhitespace:
                            xWriter.WriteWhitespace(xReader.Value);
                            break;
                        case XmlNodeType.Text:
                            xWriter.WriteString(xReader.Value);
                            break;
                        case XmlNodeType.Whitespace:
                            xWriter.WriteWhitespace(xReader.Value);
                            break;
                    }
//.........这里部分代码省略.........
开发者ID:rcurlette,项目名称:DataExtenderMetadataCol,代码行数:101,代码来源:AddMetadataColumn.cs

示例4: WriteNode

 // Writes the current node into the provided XmlWriter.
 private void WriteNode(XmlTextWriter xtw, bool defattr) {
     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);
                 xtw.QuoteChar = this.QuoteChar;
                 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();
     }
 }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:45,代码来源:xmlreader.cs

示例5: InjectInXml

 public static string InjectInXml(string XML, int InjectionPosition, string Payload)
 {
     //StringBuilder OutXml = new StringBuilder();
     StringReader XMLStringReader = new StringReader(XML.Trim());
     XmlReader Reader = XmlReader.Create(XMLStringReader);
     //XmlWriter Writer = XmlWriter.Create(OutXml);
     StringWriter OutXml = new StringWriter();
     XmlTextWriter Writer = new XmlTextWriter(OutXml);
     int ParameterCount = 0;
     bool Read = true;
     bool NextRead = false;
     while (Read)
     {
         if (!NextRead) Read = Reader.Read();
         NextRead = false;
         if (!Read) continue;
         if (Reader.IsStartElement())
         {
             Writer.WriteStartElement(Reader.Name);
             if(Reader.HasAttributes) Writer.WriteAttributes(Reader, false);
             Read = Reader.Read();
             if (Reader.NodeType == XmlNodeType.EndElement)
             {
                 if (ParameterCount == InjectionPosition)
                     Writer.WriteString(Payload);
                 else
                     Writer.WriteString("");
                 ParameterCount++;
                 Writer.WriteEndElement();
             }
             else
             {
                 NextRead = true;
             }
         }
         else
         {
             if (Reader.NodeType == XmlNodeType.EndElement)
             {
                 Writer.WriteEndElement();
             }
             else
             {
                 if (ParameterCount == InjectionPosition)
                     Writer.WriteString(Payload);
                 else
                     Writer.WriteString(Reader.Value);
                 ParameterCount++;
             }
         }
     }
     Reader.Close();
     Writer.Close();
     OutXml.Close();
     //string OutXmlString = OutXml.ToString().Split(new string[] { "?>" }, 2, StringSplitOptions.None)[1];
     return OutXml.ToString();
 }
开发者ID:kartikeyap,项目名称:IronWASP,代码行数:57,代码来源:FormatPlugin.cs

示例6: VoterExportXMLButton_Click

        private void VoterExportXMLButton_Click(object sender, System.EventArgs e)
        {
            DateTime startDate;
            DateTime endDate;
            if (rdAllDates.Checked)
            {
                startDate = new DateTime(2004, 1, 1);
                endDate = DateTime.Today;
            }
            else
            {
                startDate = (StartDateTextBox.Text == null && !Information.IsDate(StartDateTextBox.Text)) ?
                     new DateTime(2004, 1, 1) : DateTime.Parse(StartDateTextBox.Text);
                endDate = (EndDateTextBox.Text == null && !Information.IsDate(EndDateTextBox.Text)) ?
                     new DateTime(2010, 1, 1) : DateTime.Parse(EndDateTextBox.Text);
            }

            Response.Charset = "utf-8";
            Response.ContentType = "text/xml";
            Response.AddHeader("Content-Disposition", "attachment; filename=\"survey_results" + SurveyId + ".xml\"");

            NSurveyVoter voterAnswers = new Voters().GetForExport(SurveyId, startDate, endDate);


            WebSecurityAddInCollection securityAddIns = WebSecurityAddInFactory.CreateWebSecurityAddInCollection(new SecurityAddIns().GetWebSecurityAddIns(SurveyId), ViewState, null);
            NameValueCollection addInVoterData;

            if (voterAnswers.Voter.Rows.Count > 0)
            {
                // Get security addins data if any available and get key name
                // to add it as new column to the voter table
                for (int i = 0; i < securityAddIns.Count; i++)
                {
                    addInVoterData = securityAddIns[i].GetAddInVoterData(voterAnswers.Voter[0].VoterID);
                    if (addInVoterData != null)
                    {
                        for (int j = 0; j < addInVoterData.Count; j++)
                        {
                            voterAnswers.Voter.Columns.Add(new DataColumn(addInVoterData.GetKey(j), typeof(string), null, System.Data.MappingType.Element));
                        }
                    }
                }

                for (int voterIndex = 0; voterIndex < voterAnswers.Voter.Rows.Count; voterIndex++)
                {
                    // Get security addins data if any available
                    for (int i = 0; i < securityAddIns.Count; i++)
                    {
                        addInVoterData =
                            securityAddIns[i].GetAddInVoterData(((NSurveyVoter.VoterRow)voterAnswers.Voter.Rows[voterIndex]).VoterID);
                        if (addInVoterData != null)
                        {
                            for (int j = 0; j < addInVoterData.Count; j++)
                            {
                                voterAnswers.Voter.Rows[voterIndex][addInVoterData.GetKey(j)] = addInVoterData[j];
                            }
                        }
                    }
                }
            }


            XmlReader reader = new XmlTextReader(voterAnswers.GetXml(), XmlNodeType.Document, null); ;
            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8); ;

            writer.WriteStartDocument(true);

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        writer.WriteStartElement(reader.Prefix,
                            reader.LocalName, reader.NamespaceURI);
                        if (reader.HasAttributes)
                        {
                            writer.WriteAttributes(reader, true);
                            reader.MoveToElement();
                        }
                        if (reader.IsEmptyElement)
                            writer.WriteEndElement();
                        break;
                    case XmlNodeType.EndElement:
                        writer.WriteEndElement();
                        break;
                    case XmlNodeType.Text:
                        // removes any invalid xml char
                        writer.WriteString(reader.Value.Replace(((char)12).ToString(), ""));
                        break;
                    case XmlNodeType.CDATA:
                        writer.WriteCData(reader.Value);
                        break;
                }
            }
            writer.WriteEndDocument();

            writer.Close();
            reader.Close();

            Response.End();
//.........这里部分代码省略.........
开发者ID:ChrisNelsonPE,项目名称:surveyproject_main_public,代码行数:101,代码来源:ExportData.aspx.cs

示例7: WriteAttributes

		public void WriteAttributes ()
		{
			XmlDocument doc = new XmlDocument();
			StringWriter sw = new StringWriter();
			XmlWriter wr = new XmlTextWriter(sw);
			StringBuilder sb = sw.GetStringBuilder();
			XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
			XmlTextReader xtr = new XmlTextReader("<?xml version='1.0' encoding='utf-8' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);

			xtr.Read();	// read XMLDecl
			wr.WriteAttributes(xtr, false);
			// This method don't always have to take this double-quoted style...
			Assert.AreEqual ("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString ().Trim (),
				"#WriteAttributes.XmlDecl.1");

			sb.Remove(0, sb.Length);	// init
			ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
			xtr = new XmlTextReader("<?xml version='1.0'		 standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
			xtr.Read();	// read XMLDecl
			Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
			sw = new StringWriter ();
			wr = new XmlTextWriter (sw);

			// This block raises an error on MS.NET 1.0.
			wr.WriteAttributes(xtr, false);
			// This method don't always have to take this double-quoted style...
			Assert.AreEqual ("version=\"1.0\" standalone=\"no\"", sw.ToString ().Trim (),
				"#WriteAttributes.XmlDecl.2");

			sw = new StringWriter ();
			wr = new XmlTextWriter (sw);
			sb.Remove(0, sb.Length);	// init

			xtr.Read();	// read root
			Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
			wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
			wr.WriteAttributes(xtr, false);
			wr.WriteEndElement();
			wr.Close();
			// This method don't always have to take this double-quoted style...
			Assert.AreEqual ("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString ().Trim (),
				"#WriteAttributes.Element");
			xtr.Close ();
		}
开发者ID:Profit0004,项目名称:mono,代码行数:44,代码来源:XmlTextWriterTests.cs

示例8: InjectInXml

        public static string InjectInXml(string XML, int InjectionPosition, string Payload)
        {
            //StringBuilder OutXml = new StringBuilder();
            StringReader XMLStringReader = new StringReader(XML.Trim());
            XmlReader Reader = XmlReader.Create(XMLStringReader);
            //XmlWriter Writer = XmlWriter.Create(OutXml);
            StringWriter OutXml = new StringWriter();
            XmlTextWriter Writer = new XmlTextWriter(OutXml);
            Writer.Formatting = Formatting.Indented;
            int ParameterCount = 0;
            bool Read = true;
            //bool NextRead = false;
            while (Read)
            {
                //if (!NextRead) Read = Reader.Read();
                //NextRead = false;
                Read = Reader.Read();
                if (!Read) continue;
                //while (Reader.NodeType == XmlNodeType.Whitespace || Reader.NodeType == XmlNodeType.SignificantWhitespace)
                //{
                //    Reader.Read();
                //}
                if (Reader.IsStartElement())
                {
                    Writer.WriteStartElement(Reader.Name);
                    if(Reader.HasAttributes) Writer.WriteAttributes(Reader, false);
                    if (Reader.IsEmptyElement)
                    {
                        if (ParameterCount == InjectionPosition)
                            Writer.WriteString(Payload);
                        else
                            Writer.WriteString(Reader.Value);
                        ParameterCount++;
                        Writer.WriteEndElement();
                    }

                    //Read = Reader.Read();
                    //while (Reader.NodeType == XmlNodeType.Whitespace || Reader.NodeType == XmlNodeType.SignificantWhitespace)
                    //{
                    //    Reader.Read();
                    //}
                    //if (Reader.NodeType == XmlNodeType.Text || Reader.NodeType == XmlNodeType.EndElement)
                    //{
                    //    if (ParameterCount == InjectionPosition)
                    //        Writer.WriteString(Payload);
                    //    else
                    //        Writer.WriteString(Reader.Value);
                    //    ParameterCount++;
                    //}
                    //else
                    //{
                    //    NextRead = true;
                    //}
                    //if (Reader.NodeType == XmlNodeType.EndElement)
                    //{
                    //    //if (ParameterCount == InjectionPosition)
                    //    //    Writer.WriteString(Payload);
                    //    //else
                    //    //    Writer.WriteString("");
                    //    //ParameterCount++;
                    //    Writer.WriteEndElement();
                    //}
                }
                else if (Reader.NodeType == XmlNodeType.Text)
                {
                    if (ParameterCount == InjectionPosition)
                        Writer.WriteString(Payload);
                    else
                        Writer.WriteString(Reader.Value);
                    ParameterCount++;
                }
                else if (Reader.NodeType == XmlNodeType.Whitespace || Reader.NodeType == XmlNodeType.SignificantWhitespace)
                {
                }
                else if (Reader.NodeType == XmlNodeType.EndElement)
                {
                    Writer.WriteEndElement();
                }
                //else
                //{
                //    if (Reader.NodeType == XmlNodeType.EndElement)
                //    {
                //        Writer.WriteEndElement();
                //    }
                //    //else
                //    //{
                //    //    if (ParameterCount == InjectionPosition)
                //    //        Writer.WriteString(Payload);
                //    //    else
                //    //        Writer.WriteString(Reader.Value);
                //    //    ParameterCount++;
                //    //}
                //}
            }
            Reader.Close();
            Writer.Close();
            OutXml.Close();
            //string OutXmlString = OutXml.ToString().Split(new string[] { "?>" }, 2, StringSplitOptions.None)[1];
            return OutXml.ToString();
        }
开发者ID:0ks3ii,项目名称:IronWASP,代码行数:100,代码来源:FormatPlugin.cs

示例9: selectfeatures

        public static void selectfeatures(Pair<string, float>[] featureweights, Pair<string, string>[] labels, string inputFile, string outputFile)
        {
            orderfeatures(featureweights, inputFile);
            opgroup[] featureIdx = new opgroup[featureweights.Length];
            for (int i = 0; i < featureweights.Length; ++i)
            {
                featureIdx[i] = new opgroup(featureweights[i].first);
            }
            globvars.labels = labels;
            SetMatchAttrFile(inputFile, featureIdx);
            FileStream fso = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
            StreamWriter tfso = new StreamWriter(fso);
            FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
            XmlTextReader xi = new XmlTextReader(fs);
            XmlTextWriter xo = new XmlTextWriter(tfso);
            xo.Indentation = 1;
            xo.IndentChar = '\t';
            xo.Formatting = Formatting.Indented;
            while (xi.Read())
            {
                if (xi.NodeType == XmlNodeType.Element && xi.IsStartElement())
                {
                    if (xi.Name == "dataset")
                    {
                        xo.WriteStartElement(xi.Name); // dataset
                        xo.WriteAttributes(xi, true);
                        break;
                    }
                }
                else
                {
                    xo.WriteNode(xi, true);
                }
            }
            xo.WriteStartElement("header");
            xo.WriteStartElement("attributes");
            for (int i = 0; i < featureweights.Length; ++i)
            {
                string s = featureweights[i].first;
                float curweight = featureweights[i].second;
                xo.WriteStartElement("attribute");
                if (s == "class")
                {
                    xo.WriteAttributeString("class", "yes");
                    xo.WriteAttributeString("name", "class");
                    xo.WriteAttributeString("type", "nominal");
                    xo.WriteStartElement("labels");
                    foreach (string x in labels.GetUnique())
                    {
                        xo.WriteElementString("label", x);
                    }
                    xo.WriteEndElement(); // labels
                    xo.WriteEndElement(); // attribute
                }
                else
                {
                    xo.WriteAttributeString("name", s);
                    xo.WriteAttributeString("type", "numeric");
                    xo.WriteStartElement("metadata");
                    xo.WriteStartElement("property");
                    xo.WriteAttributeString("name", "weight");
                    xo.WriteValue(curweight);
                    xo.WriteEndElement(); // property
                    xo.WriteEndElement(); // metadata
                    xo.WriteEndElement(); // attribute
                }
            }

            while (xi.Read())
            {
                if (xi.NodeType == XmlNodeType.EndElement)
                {
                    if (xi.Name == "attributes")
                    {
                        xo.WriteEndElement();
                    }
                    else if (xi.Name == "header")
                    {
                        xo.WriteEndElement();
                        break;
                    }
                }
            }
            while (xi.Read())
            {
                if (xi.NodeType == XmlNodeType.Element && xi.IsStartElement())
                {
                    xo.WriteStartElement(xi.Name);
                    xo.WriteAttributes(xi, true);
                    if (xi.Name == "instances")
                        break;
                }
            }
            int vcount = 0;
            while (xi.Read())
            {
                if (xi.Name == "instance")
                {
                    if (xi.IsStartElement())
                    {
//.........这里部分代码省略.........
开发者ID:gkovacs,项目名称:xrff-utils,代码行数:101,代码来源:Main.cs

示例10: WriteHeader

 public static void WriteHeader(XmlTextReader xi, XmlTextWriter xo)
 {
     while (xi.Read())
     {
         if (xi.NodeType == XmlNodeType.Element && xi.IsStartElement())
         {
             if (xi.Name == "dataset")
             {
                 xo.WriteStartElement(xi.Name); // dataset
                 xo.WriteAttributes(xi, true);
                 break;
             }
         }
         else
         {
             xo.WriteNode(xi, true);
         }
     }
     while (xi.Read())
     {
         if (xi.IsStartElement())
         {
             if (xi.Name == "header")
             {
                 xo.WriteStartElement(xi.Name);
                 xo.WriteAttributes(xi, true);
                 break;
             }
         }
     }
     while (xi.Read())
     {
         if (xi.IsStartElement())
         {
             if (xi.Name == "attributes")
             {
                 xo.WriteNode(xi, true);
                 xo.WriteEndElement(); // header
                 break;
             }
         }
     }
     while (xi.Read())
     {
         if (xi.IsStartElement())
         {
             xo.WriteStartElement(xi.Name);
             xo.WriteAttributes(xi, true);
             if (xi.Name == "instances")
             {
                 break;
             }
         }
     }
 }
开发者ID:gkovacs,项目名称:xrff-utils,代码行数:55,代码来源:Main.cs


注:本文中的System.Xml.XmlTextWriter.WriteAttributes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。