TheMessiah
❔ Using XML to modify Nuget nuspec files
I am trying to take data from my old nuget packages nuspec file
then I want to get the data from each tag which I have already done
but when I go to take the data and add it into a new nuspec file it does not work at all
For some reason none of the tags get edited
public static string ReplaceNuspecValues(string nuspecContent, string newId, string newVersion, string newAuthors, string newTags, string newProjectUrl, string newDescription)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(nuspecContent);
XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable);
xnm.AddNamespace("ns", "http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd");
UpdateInnerText(xmlDoc, "//ns:id/text()", xnm, newId);
UpdateInnerText(xmlDoc, "//ns:version/text()", xnm, newVersion);
UpdateInnerText(xmlDoc, "//ns:authors/text()", xnm, newAuthors);
UpdateInnerText(xmlDoc, "//ns:tags/text()", xnm, newTags);
UpdateInnerText(xmlDoc, "//ns:projectUrl/text()", xnm, newProjectUrl);
UpdateInnerText(xmlDoc, "//ns:description/text()", xnm, newDescription);
return xmlDoc.OuterXml;
}
public static void UpdateInnerText(XmlDocument xmlDoc, string xpath, XmlNamespaceManager xnm, string newValue)
{
XmlNode node = xmlDoc.SelectSingleNode(xpath, xnm);
if (node != null)
{
node.InnerText = newValue;
}
}
public static string ReplaceNuspecValues(string nuspecContent, string newId, string newVersion, string newAuthors, string newTags, string newProjectUrl, string newDescription)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(nuspecContent);
XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable);
xnm.AddNamespace("ns", "http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd");
UpdateInnerText(xmlDoc, "//ns:id/text()", xnm, newId);
UpdateInnerText(xmlDoc, "//ns:version/text()", xnm, newVersion);
UpdateInnerText(xmlDoc, "//ns:authors/text()", xnm, newAuthors);
UpdateInnerText(xmlDoc, "//ns:tags/text()", xnm, newTags);
UpdateInnerText(xmlDoc, "//ns:projectUrl/text()", xnm, newProjectUrl);
UpdateInnerText(xmlDoc, "//ns:description/text()", xnm, newDescription);
return xmlDoc.OuterXml;
}
public static void UpdateInnerText(XmlDocument xmlDoc, string xpath, XmlNamespaceManager xnm, string newValue)
{
XmlNode node = xmlDoc.SelectSingleNode(xpath, xnm);
if (node != null)
{
node.InnerText = newValue;
}
}
2 replies