Awesome WinUI (edit)
WinUI (C#)
A collection of awesome resources for WinUI / Windows developers
scottkuhl/awesome-winui: A collection of awesome resources for WinUI / Windows developers. (github.com)
.NET Utilities (C#)
A general purpose utility and helper library for .NET development
RickStrahl/Westwind.Utilities: A general purpose utility and helper library for .NET development (github.com)
Working with XML Document (XML Utils) (XmlUtils)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace XmlGenCodeTool
{
[XmlRoot("MyDocument", Namespace = "http://www.dotnetcoretutorials.com/namespace")]
public class MyDocument
{
public string MyProperty { get; set; }
public MyAttributeProperty MyAttributeProperty { get; set; }
[XmlArray]
[XmlArrayItem(ElementName = "MyListItem")]
public List<string> MyList { get; set; }
}
public class MyAttributeProperty
{
[XmlAttribute("value")]
public int AValue { get; set; }
}
class Program
{
/// <summary>
/// https://dotnetcoretutorials.com/2020/04/23/how-to-parse-xml-in-net-core/
/// https://stackoverflow.com/questions/1880379/avoid-exception-on-xml-selectsinglenode-function
/// https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.appendchild?view=net-5.0
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
#region Read XML
string path = "Test1.xml";
using (var fileStream = File.Open(path, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyDocument));
var myDocument = (MyDocument)serializer.Deserialize(fileStream);
Console.WriteLine(string.Format("My Property : {0}", myDocument.MyProperty));
Console.WriteLine(string.Format("My Attribute : {0}", myDocument.MyAttributeProperty.AValue));
foreach (var item in myDocument.MyList)
{
Console.WriteLine(item);
}
}
#endregion Read XML
#region Add new element
path = "Test2.xml";
XDocument doc = XDocument.Load(path);
XElement root = new XElement("Snippet");
root.Add(new XAttribute("name", "name goes here"));
root.Add(new XElement("SnippetCode", "SnippetCode"));
doc.Element("Snippets").Add(root);
doc.Save(path);
#endregion
#region Update Element
path = "Test3.xml";
string nodeId = "2";
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc2.Load(path);
XmlNode node = xmlDoc2.SelectSingleNode("/Projects/Project[@ID=" + nodeId + "]");
node.Attributes["Name"].Value = "Project2_Update";
xmlDoc2.Save(path);
#endregion Update Element
#region Insert Element
// Input
/*
<Project>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>TRACE;DEBUG;</DefineConstants>
</PropertyGroup>
</Project>
*/
// Output:
/*
<Project>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>TRACE;DEBUG;</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
</Project>
*/
path = @"C:\Git\Westwind.Utilities\Westwind.Utilities\Westwind.Utilities.csproj";
string attrName = "Condition";
string attrValue = "'$(Configuration)'=='Debug'";
string xmlString = File.ReadAllText(path, Encoding.UTF8);
XmlDocument xmlProject = new XmlDocument();
xmlProject.LoadXml(xmlString); // <PropertyGroup Condition="'$(Configuration)'=='Debug'">
//Find the specify node search by condition
var nodeList1 = xmlProject.SelectNodes("/Project/PropertyGroup");
for (int i = 0; i < nodeList1.Count; i++)
{
var node2 = nodeList1[i];
if (IsNodeAttributeEqualsSpecifyValue(node2, attrName, attrValue))
{
var nodeList2 = node2.SelectNodes("/Project/PropertyGroup/PlatformTarget");
var found = false;
for (int j = 0; j < nodeList2.Count; j++)
{
var node3 = nodeList2[j].ParentNode;
if (IsNodeAttributeEqualsSpecifyValue(node3, attrName, attrValue))
{
found = true;
break;
}
}
if (!found)
{
//Create a new node
XmlElement elem = xmlProject.CreateElement("PlatformTarget");
elem.InnerText = "x64";
//Add the node to the specify node
node2.AppendChild(elem);
}
break;
}
}
xmlProject.Save(path);
#endregion Insert Element
Console.WriteLine("Hello World");
}
private static bool IsNodeAttributeEqualsSpecifyValue(XmlNode parentNode, string attributeName, string attributeValue)
{
// <PropertyGroup Condition="'$(Configuration)'=='Debug'">
return parentNode.Attributes[attributeName] != null && parentNode.Attributes[attributeName].Value == attributeValue;
}
}
}