public static void ParseProjectFile(string path, References dllName) { StreamReader reader = null; //StreamWriter writer = null; try { reader = new StreamReader(path); //writer = new StreamWriter(path +".TEMP"); bool containsname = false; string line, prevline = null; MemoryStream stream = new MemoryStream(); reader.BaseStream.CopyTo(stream); stream.Position = 0; reader.BaseStream.Position = 0; var string1 = new StreamReader(stream).ReadToEnd(); while ((line = reader.ReadLine()) != null) { if (line.Contains(dllName.OldPath)) { containsname = true; //var index = string1.IndexOf(prevline); string1 = GetXmlFile(string1, dllName, path); string1 = string1.Replace(@"utf-16", "utf-8"); //string1 = ReplaceReference(string1, xmltag); //var index = string1.IndexOf(line, StringComparison.Ordinal); //var nextline = reader.ReadLine(); //if(nextline != null) //{ // var indexOfRef = string1.IndexOf("\n", index, StringComparison.Ordinal); // var indexofnextline = string1.IndexOf("\n", indexOfRef+1, StringComparison.Ordinal); // string1 = string1.Remove(indexOfRef, indexofnextline-indexOfRef); //} ////GetXmlFile(string1, line); //string1 = prevline != null ? ReplaceReference(string1, prevline) : string1; //string1 = ReplaceReference(string1, line); logger1.Info($"{dllName}"); break; } if (!line.Contains("SpecificVersion")) { prevline = line; } } reader?.Close(); stream?.Close(); if (containsname) { var streamwriter = new StreamWriter(path); var task = streamwriter.WriteAsync(string1); task.Wait(); streamwriter.Close(); } //writer.Close(); //if (containsname) //{ // File.Delete(path); // File.Copy(path + ".TEMP", path); // File.Delete(path + ".TEMP"); //} //else //{ // File.Delete(path + ".TEMP"); //} } catch (Exception ex) { logger.Error(ex.Message); } finally { reader?.Close(); } }
private static string GetXmlFile(string data, References dllname, string path) { var stream = new MemoryStream(); var doc = new XmlDocument(); doc.LoadXml(data); doc.Save(stream); stream.Position = 0; bool imported = false; //var nodes = doc?.DocumentElement?.SelectNodes("/Project/ItemGroup/Reference"); foreach (XmlNode childnodes in doc.ChildNodes) { foreach (XmlNode node in childnodes.ChildNodes) { foreach (XmlNode reference in node.ChildNodes) { if (reference.InnerText.Contains(dllname.OldPath)) { reference.ParentNode?.RemoveChild(reference); //reference.InnerXml = String.Empty; //reference.RemoveAll(); break; } if (reference.Name == "ProjectReference" && !imported) { var doc1 = new XmlDocument(); var child = LoadNewProjectDetails(dllname.NewPath, path); doc1.LoadXml(GetProjectReference(child)); XmlNode importNode = reference.OwnerDocument?.ImportNode(doc1.ChildNodes[0], true); if (importNode != null) { reference.ParentNode?.AppendChild(importNode); imported = true; } } } } } doc.InnerXml = doc.InnerXml.Replace(@" xmlns=""""", string.Empty); return(GetProjectReference(doc, false)); //if (nodes == null) //{ // return string.Empty; //} //foreach (XmlNode node in nodes) //{ // if (node.InnerText.Contains(oldPath)) // { // node.RemoveAll(); // } //} //Project obj = (Project)new XmlSerializer(typeof(Project), new XmlRootAttribute() { Namespace = "http://schemas.microsoft.com/developer/msbuild/2003" }).Deserialize(stream); //foreach (var node in obj.ItemGroup) //{ // var ss1 = node.ProjectReference.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.HintPath) && x.HintPath.Contains(oldPath)); // if (ss1 != null) // { // var xmva = GetProjectReference(ss1); // doc.InnerXml = doc.InnerXml.Replace(xmva, ""); // xmva = GetProjectReference(doc); // return xmva; // } //} //return null; }
public static void ParseDllFile(string path, References dllName) { StreamReader reader = null; //StreamWriter writer = null; try { reader = new StreamReader(path); //writer = new StreamWriter(path +".TEMP"); bool containsname = false; string line, prevline = null; MemoryStream stream = new MemoryStream(); reader.BaseStream.CopyTo(stream); stream.Position = 0; reader.BaseStream.Position = 0; var string1 = new StreamReader(stream).ReadToEnd(); while ((line = reader.ReadLine()) != null) { if (line.Contains(dllName.OldPath)) { containsname = true; //var index = string1.IndexOf(prevline); //var stream = new MemoryStream(); var doc = new XmlDocument(); doc.LoadXml(string1); doc.Save(stream); stream.Position = 0; bool imported = false; //var nodes = doc?.DocumentElement?.SelectNodes("/Project/ItemGroup/Reference"); foreach (XmlNode childnodes in doc.ChildNodes) { foreach (XmlNode node in childnodes.ChildNodes) { foreach (XmlNode reference in node.ChildNodes) { if (reference.InnerText.Contains(dllName.OldPath)) { //reference.ParentNode?.RemoveChild(reference); //reference.InnerXml = String.Empty; //reference.RemoveAll(); break; } } } } doc.InnerXml = doc.InnerXml.Replace(@" xmlns=""""", string.Empty); string1 = GetProjectReference(doc, false); string1 = string1.Replace(@"utf-16", "utf-8"); logger1.Info($"{dllName}"); break; } if (!line.Contains("SpecificVersion")) { prevline = line; } } reader?.Close(); stream?.Close(); if (containsname) { var streamwriter = new StreamWriter(path); var task = streamwriter.WriteAsync(string1); task.Wait(); streamwriter.Close(); } //writer.Close(); //if (containsname) //{ // File.Delete(path); // File.Copy(path + ".TEMP", path); // File.Delete(path + ".TEMP"); //} //else //{ // File.Delete(path + ".TEMP"); //} } catch (Exception ex) { logger.Error(ex.Message); } finally { reader?.Close(); } }