Simple console dotnet application that shows xml manipulation.
Program.cs
-
using System;
-
using System.IO;
-
using System.Xml;
-
using System.Xml.Linq;
-
using System.Xml.XPath;
-
using System.Linq;
-
using System.Collections.Generic;
-
-
namespace HelloWorld
-
{
-
class NSPrefixMapping
-
{
-
public string Prefix;
-
public string Namespace;
-
public NSPrefixMapping(string name, string ns)
-
{
-
Prefix = name;
-
Namespace = ns;
-
}
-
}
-
class XPathValuePair
-
{
-
public string Name;
-
public string XPath;
-
public string Value;
-
public XPathValuePair(string name, string xpath, string value)
-
{
-
Name = name;
-
XPath = xpath;
-
Value = value;
-
}
-
public override string ToString()
-
{
-
return "Name: " + Name + " value: " + Value;
-
}
-
}
-
-
class Program
-
{
-
static string exampleXML = "<main xmlns=\"test\" xmlns:it=\"testNS2\"><it:a>A</it:a><it:b>B</it:b><it:c>C</it:c></main>";
-
static string exampleNestedXML = "<main xmlns=\"test\" xmlns:it=\"testNS2\"><it:a>A</it:a><it:items><it:item><it:name>Fir1</it:name><it:cost>121</it:cost></it:item><it:item><it:name>Fir2</it:name><it:cost>122</it:cost></it:item><it:item><it:name>Fir3</it:name><it:cost>123</it:cost></it:item></it:items><it:c>C</it:c></main>";
-
-
static List<XPathValuePair> makeNameValueStructureFromXMLStringForOutsystems(string origXML, List<XPathValuePair> xpathsToLookup, List<NSPrefixMapping> nsprefixmappings)
-
{
-
XDocument xd = XDocument.Parse(origXML);
-
var xmlReader = XmlReader.Create( new StringReader(origXML) );
-
var namespaceManager = new XmlNamespaceManager( xmlReader.NameTable );
-
foreach (NSPrefixMapping nsprefixmapping in nsprefixmappings) {
-
namespaceManager.AddNamespace( nsprefixmapping.Prefix, nsprefixmapping.Namespace );
-
}
-
-
List<XPathValuePair> ret = new List<XPathValuePair>();
-
foreach (XPathValuePair xpathValuePair in xpathsToLookup) {
-
string lookedUpValue = xd.XPathSelectElement(xpathValuePair.XPath, namespaceManager).Value;
-
ret.Add(new XPathValuePair(xpathValuePair.Name, xpathValuePair.XPath, lookedUpValue));
-
}
-
return ret;
-
}
-
-
static string makeNewXMLWithNewValues(string origXML, List<XPathValuePair> xpathsToLookup, List<NSPrefixMapping> nsprefixmappings)
-
{
-
XDocument xd = XDocument.Parse(origXML);
-
var xmlReader = XmlReader.Create( new StringReader(origXML) );
-
var namespaceManager = new XmlNamespaceManager( xmlReader.NameTable );
-
foreach (NSPrefixMapping nsprefixmapping in nsprefixmappings) {
-
namespaceManager.AddNamespace( nsprefixmapping.Prefix, nsprefixmapping.Namespace );
-
}
-
-
List<XPathValuePair> ret = new List<XPathValuePair>();
-
foreach (XPathValuePair xpathValuePair in xpathsToLookup) {
-
xd.XPathSelectElement(xpathValuePair.XPath, namespaceManager).Value = xpathValuePair.Value;
-
}
-
return xd.ToString();
-
}
-
-
static void testXML(string xml, string xpathToChange)
-
{
-
Console.WriteLine(xml);
-
XDocument xd = XDocument.Parse(xml);
-
var xmlReader = XmlReader.Create( new StringReader(xml) );
-
var namespaceManager = new XmlNamespaceManager( xmlReader.NameTable );
-
namespaceManager.AddNamespace( "prefix", "test" );
-
namespaceManager.AddNamespace( "prefix2", "testNS2" );
-
-
Console.WriteLine("Original doc:" + xd.ToString());
-
xd.XPathSelectElement(xpathToChange, namespaceManager).Value = "AlteredValue";
-
Console.WriteLine("Element B Altered doc:" + xd.ToString());
-
}
-
-
static void Main(string[] args)
-
{
-
Console.WriteLine("Hello World!");
-
-
XDocument xd = XDocument.Parse(exampleNestedXML);
-
Console.WriteLine("Example Doc\n" + xd.ToString());
-
-
-
// Set up some sample XPaths to lookup
-
List<NSPrefixMapping> namespaces = new List<NSPrefixMapping>();
-
namespaces.Add(new NSPrefixMapping("prefix", "test"));
-
namespaces.Add(new NSPrefixMapping("prefix2", "testNS2"));
-
-
List<XPathValuePair> sampleXPathsForThisEBO = new List<XPathValuePair>();
-
sampleXPathsForThisEBO.Add(new XPathValuePair("Fir2Cost", "/prefix:main/prefix2:items/prefix2:item[prefix2:name='Fir2']/prefix2:cost", ""));
-
sampleXPathsForThisEBO.Add(new XPathValuePair("A", "/prefix:main/prefix2:a", ""));
-
-
List<XPathValuePair> lookedupValues = makeNameValueStructureFromXMLStringForOutsystems(exampleNestedXML, sampleXPathsForThisEBO, namespaces);
-
-
Console.WriteLine("Values read from XML:");
-
foreach (XPathValuePair xpathValuePair in lookedupValues) {
-
Console.WriteLine(xpathValuePair.ToString());
-
}
-
-
Console.WriteLine("******************************************");
-
-
-
// Assume we want to change the data
-
List<XPathValuePair> newValues = new List<XPathValuePair>();
-
newValues.Add(new XPathValuePair(lookedupValues.ElementAt(0).Name, lookedupValues.ElementAt(0).XPath, "999"));
-
newValues.Add(new XPathValuePair(lookedupValues.ElementAt(1).Name, lookedupValues.ElementAt(1).XPath, "Altered ValueXXX"));
-
-
Console.WriteLine("User changed values to:");
-
foreach (XPathValuePair xpathValuePair in newValues) {
-
Console.WriteLine(xpathValuePair.ToString());
-
}
-
-
Console.WriteLine("Altered XML\n" + makeNewXMLWithNewValues(exampleNestedXML, newValues, namespaces));
-
-
-
// testXML(exampleXML, "/prefix:main/prefix2:b");
-
// testXML(exampleNestedXML, "/prefix:main/prefix2:items/prefix2:item[prefix2:name='Fir2']/prefix2:cost");
-
-
}
-
}
Run it with:
-
dotnet run
This is the HelloWorld.csproj file:
-
<Project Sdk="Microsoft.NET.Sdk">
-
-
<PropertyGroup>
-
<OutputType>Exe</OutputType>
-
<TargetFramework>netcoreapp2.2</TargetFramework>
-
</PropertyGroup>
-
-
<ItemGroup>
-
<PackageReference Include="System.Xml.XDocument" Version="4.3.0" />
-
</ItemGroup>
-
</Project>
RJM Article Type
Work Notes