public void ReplaceWritingSystemId(string oldId, string newId)
		{
			var fileToBeWrittenTo = new TempFile();
			var reader = XmlReader.Create(_liftFilePath, CanonicalXmlSettings.CreateXmlReaderSettings());
			var writer = XmlWriter.Create(fileToBeWrittenTo.Path, CanonicalXmlSettings.CreateXmlWriterSettings());
			//System.Diagnostics.Process.Start(fileToBeWrittenTo.Path);
			try
			{
				bool readerMovedByXmlDocument = false;
				while (readerMovedByXmlDocument || reader.Read())
				{
					readerMovedByXmlDocument = false;
					var xmldoc = new XmlDocument();
					//We load up the header and entry nodes individually as XmlDocuments and replace the writing systems
					//This is not as fast as pure reader writers, but as this is not a frequent operation, that is ok and
					//it is MUCH easier to code than a statemachine using readers and writers only.
					if (reader.NodeType == XmlNodeType.Element && reader.Name == "header")
					{
						var headerFragment = xmldoc.ReadNode(reader);
						readerMovedByXmlDocument = true;
						GetNodeWithWritingSystemIdsReplaced(oldId, newId, headerFragment);
						headerFragment.WriteTo(writer);
					}
					if (reader.NodeType == XmlNodeType.Element && reader.Name == "entry")
					{
						var entryFragment = xmldoc.ReadNode(reader);
						readerMovedByXmlDocument = true;
						GetNodeWithWritingSystemIdsReplaced(oldId, newId, entryFragment);
						entryFragment.WriteTo(writer);
					}
					else
					{
						writer.WriteNodeShallow(reader);
					}
					//writer.Flush();
				}
			}
			finally
			{
				reader.Close();
				writer.Close();
			}
			File.Delete(_liftFilePath);
			fileToBeWrittenTo.MoveTo(_liftFilePath);
		}
		public void DeleteWritingSystemId(string id)
		{
			var fileToBeWrittenTo = new TempFile();
			var reader = XmlReader.Create(_liftFilePath, CanonicalXmlSettings.CreateXmlReaderSettings());
			var writer = XmlWriter.Create(fileToBeWrittenTo.Path, CanonicalXmlSettings.CreateXmlWriterSettings());
			//System.Diagnostics.Process.Start(fileToBeWrittenTo.Path);
			try
			{
				bool readerMovedByXmlDocument = false;
				while (readerMovedByXmlDocument || reader.Read())
				{
					readerMovedByXmlDocument = false;
					var xmldoc = new XmlDocument();
					if (reader.NodeType == XmlNodeType.Element && reader.Name == "entry")
					{
						var entryFragment = xmldoc.ReadNode(reader);
						readerMovedByXmlDocument = true;
						var nodesWithLangId = entryFragment.SelectNodes(String.Format("//*[@lang='{0}']", id));
						if (nodesWithLangId != null)
						{
							foreach (XmlNode node in nodesWithLangId)
							{
								var parent = node.SelectSingleNode("parent::*");
								if (node.Name == "gloss")
								{
									parent.RemoveChild(node);
								}
								else
								{
									var siblingNodes =
										node.SelectNodes("following-sibling::form | preceding-sibling::form");
									if (siblingNodes.Count == 0)
									{
										var grandParent = parent.SelectSingleNode("parent::*");
										grandParent.RemoveChild(parent);
									}
									else
									{
										parent.RemoveChild(node);
									}
								}
							}
						}
						entryFragment.WriteTo(writer);
					}
					else
					{
						writer.WriteNodeShallow(reader);
					}
					//writer.Flush();
				}
			}
			finally
			{
				reader.Close();
				writer.Close();
			}
			File.Delete(_liftFilePath);
			fileToBeWrittenTo.MoveTo(_liftFilePath);
		}
Пример #3
0
		public void MoveTo_Path_PathPropertyIsSet()
		{
			using (TempFile temp = new TempFile())
			{
				string newPath = Path.Combine(Path.GetTempPath(), "TempFile.tmp");
				string oldPath = temp.Path;

				Assert.IsTrue(File.Exists(oldPath));
				Assert.IsFalse(File.Exists(newPath));
				temp.MoveTo(newPath);
				Assert.IsFalse(File.Exists(oldPath));
				Assert.IsTrue(File.Exists(newPath));
				Assert.AreEqual(newPath, temp.Path);
			}
		}