C#读取写入带有命名空间的XML文件

原xml文件样式

 70

 960

 920

 90000

 1sdfsda

 

   100

   1

   1

   0

 

 

   

     0

     0

   

   

     0

     0

   

   0

   0

   0

   0

 

 AllGb2312

 SimpleScreenOCR

 ColorMethod

读取XML的代码

 

public class ClassXMLRead

    {

        XmlDocumentxmlDoc = new XmlDocument();

        XmlNamespaceManagernsMgr;

       publicClassXMLRead(string filename)

       {

           nsMgr = newXmlNamespaceManager(xmlDoc.NameTable);

           xmlDoc.Load(filename);

           nsMgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

           nsMgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");

           nsMgr.AddNamespace("ns", "zqsl.screenOCR");

       }

       public XmlNode read(stringnodepath)

       {

           XmlNodenode = xmlDoc.SelectSingleNode("ns:TextParams//ns:"+ nodepath, nsMgr);

           returnnode;

       }

       public XmlNode read(stringnodepath,XmlNode node)

       {

           returnnode.SelectSingleNode("ns:" +nodepath, nsMgr); ;

       }

       public XmlNodeList readlist(stringnodepath)

       {

           XmlNodeListnode = xmlDoc.SelectNodes("ns:TextParams//ns:"+ nodepath, nsMgr);

           returnnode;

       }

      

    }

写入xml的代码

public class ClassXMLWrite

    {

        ClassParametercp;

        XmlWriterSettingsxws;

        XmlWriterxw;

        stringfilename;

        publicClassXMLWrite(string filenames)

        {

            filename = filenames;

            cp = newClassParameter();

            xws = newXmlWriterSettings();

        }

        public void xmlstart()

        {

            xws.CheckCharacters = true;

            xws.CloseOutput = true;

            xws.ConformanceLevel = ConformanceLevel.Document;

            xws.Encoding = System.Text.Encoding.UTF8;

            xws.Indent = true;

            xws.NewLineOnAttributes = true;

            xw = XmlWriter.Create(filename,xws);

            xw.WriteStartDocument();

            xw.WriteStartElement("TextParams", "zqsl.screenOCR");

            xw.WriteAttributeString("xmlns", "xsi",null, "http://www.w3.org/2001/XMLSchema-instance");

            xw.WriteAttributeString("xmlns", "xsd",null, "http://www.w3.org/2001/XMLSchema");

        }

 

        public void xmlend(ClassParameterparameter)

        {

            cp = parameter;

            xw.WriteElementString("fontCharSet", cp.FontCharSet);

            xw.WriteElementString("ocrMethod", cp.OcrMethod);

            xw.WriteElementString("extractMethod", cp.ExtractMethod);

            xw.WriteElementString("threshold", cp.Threshold);

            xw.WriteElementString("markScoreThreshold",cp.MarkScoreThreshold);

            xw.WriteElementString("chsScoreThreshold",cp.ChsScoreThreshold);

            xw.WriteElementString("charScoreThreshold",cp.CharScoreThreshold);

            xw.WriteElementString("minSegSapce", cp.MinSegSapce);

            xw.WriteFullEndElement();

            xw.Close();

        }

 

        public void textegion(ClassParameterparameter)

        {

            cp = parameter;

            xw.WriteStartElement("textRegion");

            xw.WriteStartElement("Location");

            xw.WriteElementString("X", cp.LocationX1);

            xw.WriteElementString("Y", cp.LocationY1);

            xw.WriteEndElement();

            xw.WriteStartElement("Size");

            xw.WriteElementString("Width", cp.LocationWidth1);

            xw.WriteElementString("Height", cp.LocationHeight1);

            xw.WriteEndElement();

            xw.WriteElementString("X", cp.X1);

            xw.WriteElementString("Y", cp.Y1);

            xw.WriteElementString("Width", cp.Width1);

            xw.WriteElementString("Height", cp.Height1);

            xw.WriteEndElement();

        }

 

        public void forfontcolors(ClassParameterparameter)

        {

            cp = parameter;

            xw.WriteStartElement("fontColors");

            xw.WriteElementString("Red", cp.Red1);

            xw.WriteElementString("Green", cp.Green1);

            xw.WriteElementString("Blue", cp.Blue1);

            xw.WriteElementString("ColorThrd", cp.ColorThrd1);

            xw.WriteEndElement();

        }

 

    }

Cp是我定义的一个容器


你可能感兴趣的:(XML,xml,c#,filenames,string,encoding,class)