关闭

XML读写部分代码,仅供参考!

252人阅读 评论(0) 收藏 举报

XmlDocument doc = new XmlDocument();
                doc.Load(GetXmlPath(FilePath));
                XmlNodeReader nodereader = new XmlNodeReader(doc);
               // XmlNodeList emelis = doc.SelectNodes("../w:p[text()!='']");
                XmlNodeList emelis = doc.GetElementsByTagName("w:p");
                XmlTextWriter xtw = new XmlTextWriter(XliffPath,System.Text.Encoding.Unicode);
                xtw.WriteStartDocument();


                xtw.WriteStartElement("TRANSLATEFILE");
                xtw.WriteStartElement("INSRUCTION");
                xtw.WriteAttributeString("Path", XliffPath);
                xtw.WriteAttributeString("Creator", "chenjie");   //获取创建人还需要完善
                xtw.WriteAttributeString("Date", System.DateTime.Now.ToShortDateString());
                xtw.WriteEndElement();
                xtw.WriteStartElement("ARTICLE");
              
                for (int i = 0; i < emelis.Count; i++)
                {
                    xtw.WriteStartElement("Para");
                    sentence = emelis[i].InnerText.ToString();
                    int StartPos = 0;
                    while (StartPos < sentence.Length)
                    {
                        if (IsElement == 1)
                        {
                            xtw.WriteStartElement("Tu");
                            xtw.WriteStartElement("Tuv");
                            xtw.WriteAttributeString("Language", "SourceLanguage");
                            IsElement = 0;
                        }
                        xtw.WriteString(sentence.Substring(StartPos, 1));
                        if (sentence[StartPos].ToString() == "." || sentence[StartPos].ToString() == ":" || sentence[StartPos].ToString() == "?" || sentence[StartPos].ToString() == "!" || sentence[StartPos].ToString() == "。" || sentence[StartPos].ToString() == ":" || sentence[StartPos].ToString() == "?" || sentence[StartPos].ToString() == "!")
                        {
                            IsDivideText idt = new IsDivideText();
                            if (IsElement == 0 && idt.IsDivideTag(sentence, StartPos))
                            {
                                xtw.WriteEndElement();
                                xtw.WriteStartElement("Tuv");
                                xtw.WriteAttributeString("Language", "TargetLanguage");
                                xtw.WriteEndElement();
                                xtw.WriteEndElement();
                                IsElement = 1;

                            }
                        }
                        StartPos++;
                    }
                    if (IsElement == 0)
                    {
                        xtw.WriteEndElement();
                        xtw.WriteStartElement("Tuv");
                        xtw.WriteAttributeString("Language", "TargetLanguage");
                        xtw.WriteEndElement();
                        xtw.WriteEndElement();
                        IsElement = 1;

                    }

                    xtw.WriteEndElement();

                }
                xtw.WriteEndElement();
                xtw.WriteEndElement();
                xtw.WriteEndDocument();
                xtw.Flush();
                xtw.Close();
                doc.Save(GetXmlPath(FilePath));
                File.Delete(GetXmlPath(FilePath));
            }
            catch { }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15152次
    • 积分:329
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论