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是我定义的一个容器


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值