天天看點

C#讀取寫入帶有命名空間的XML檔案

原xml檔案樣式

<?xml version="1.0"encoding="utf-8"?>

<TextParam xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="zqsl.screenOCR">

 <threshold>70</threshold>

 <markScoreThreshold>960</markScoreThreshold>

 <chsScoreThreshold>920</chsScoreThreshold>

 <charScoreThreshold>90000</charScoreThreshold>

 <minSegSapce>1sdfsda</minSegSapce>

 <fontColors>

   <Red>100</Red>

   <Green>1</Green>

   <Blue>1</Blue>

   <ColorThrd>0</ColorThrd>

 </fontColors>

  <textRegion>

   <Location>

     <X>0</X>

     <Y>0</Y>

   </Location>

   <Size>

     <Width>0</Width>

     <Height>0</Height>

   </Size>

   <X>0</X>

   <Y>0</Y>

   <Width>0</Width>

   <Height>0</Height>

 </textRegion>

 <fontCharSet>AllGb2312</fontCharSet>

 <ocrMethod>SimpleScreenOCR</ocrMethod>

 <extractMethod>ColorMethod</extractMethod>

</TextParams>

讀取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是我定義的一個容器