Dotnet XML Handling

Submitted by code_admin on Mon, 02/24/2020 - 09:15

Simple console dotnet application that shows xml manipulation.

Program.cs

  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Linq;
  5. using System.Xml.XPath;
  6. using System.Linq;
  7. using System.Collections.Generic;
  8.  
  9. namespace HelloWorld
  10. {
  11.     class NSPrefixMapping
  12.     {
  13.       public string Prefix;
  14.       public string Namespace;
  15.       public NSPrefixMapping(string name, string ns)
  16.       {
  17.         Prefix = name;
  18.         Namespace = ns;
  19.       }
  20.     }
  21.     class XPathValuePair
  22.     {
  23.       public string Name;
  24.       public string XPath;
  25.       public string Value;
  26.       public XPathValuePair(string name, string xpath, string value)
  27.       {
  28.         Name = name;
  29.         XPath = xpath;
  30.         Value = value;
  31.       }
  32.       public override string ToString()
  33.       {
  34.           return "Name: " + Name + " value: " + Value;
  35.       }
  36.     }
  37.  
  38.     class Program
  39.     {
  40.         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>";
  41.         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>";
  42.  
  43.         static List<XPathValuePair> makeNameValueStructureFromXMLStringForOutsystems(string origXML, List<XPathValuePair> xpathsToLookup, List<NSPrefixMapping> nsprefixmappings)
  44.         {
  45.           XDocument xd = XDocument.Parse(origXML);
  46.           var xmlReader = XmlReader.Create( new StringReader(origXML) );
  47.           var namespaceManager = new XmlNamespaceManager( xmlReader.NameTable );
  48.           foreach (NSPrefixMapping nsprefixmapping in nsprefixmappings) {
  49.             namespaceManager.AddNamespace( nsprefixmapping.Prefix, nsprefixmapping.Namespace );
  50.           }
  51.  
  52.           List<XPathValuePair> ret = new List<XPathValuePair>();
  53.           foreach (XPathValuePair xpathValuePair in xpathsToLookup) {
  54.             string lookedUpValue = xd.XPathSelectElement(xpathValuePair.XPath, namespaceManager).Value;
  55.             ret.Add(new XPathValuePair(xpathValuePair.Name, xpathValuePair.XPath, lookedUpValue));
  56.           }
  57.           return ret;
  58.         }
  59.  
  60.         static string makeNewXMLWithNewValues(string origXML, List<XPathValuePair> xpathsToLookup, List<NSPrefixMapping> nsprefixmappings)
  61.         {
  62.           XDocument xd = XDocument.Parse(origXML);
  63.           var xmlReader = XmlReader.Create( new StringReader(origXML) );
  64.           var namespaceManager = new XmlNamespaceManager( xmlReader.NameTable );
  65.           foreach (NSPrefixMapping nsprefixmapping in nsprefixmappings) {
  66.             namespaceManager.AddNamespace( nsprefixmapping.Prefix, nsprefixmapping.Namespace );
  67.           }
  68.  
  69.           List<XPathValuePair> ret = new List<XPathValuePair>();
  70.           foreach (XPathValuePair xpathValuePair in xpathsToLookup) {
  71.             xd.XPathSelectElement(xpathValuePair.XPath, namespaceManager).Value = xpathValuePair.Value;
  72.           }
  73.           return xd.ToString();
  74.         }
  75.  
  76.         static void testXML(string xml, string xpathToChange)
  77.         {
  78.           Console.WriteLine(xml);
  79.           XDocument xd = XDocument.Parse(xml);
  80.           var xmlReader = XmlReader.Create( new StringReader(xml) );
  81.           var namespaceManager = new XmlNamespaceManager( xmlReader.NameTable );
  82.           namespaceManager.AddNamespace( "prefix", "test" );
  83.           namespaceManager.AddNamespace( "prefix2", "testNS2" );
  84.  
  85.           Console.WriteLine("Original doc:" + xd.ToString());
  86.           xd.XPathSelectElement(xpathToChange, namespaceManager).Value = "AlteredValue";
  87.           Console.WriteLine("Element B Altered doc:" + xd.ToString());
  88.         }
  89.  
  90.         static void Main(string[] args)
  91.         {
  92.             Console.WriteLine("Hello World!");
  93.  
  94.             XDocument xd = XDocument.Parse(exampleNestedXML);
  95.             Console.WriteLine("Example Doc\n" + xd.ToString());
  96.  
  97.  
  98.             // Set up some sample XPaths to lookup
  99.             List<NSPrefixMapping> namespaces = new List<NSPrefixMapping>();
  100.             namespaces.Add(new NSPrefixMapping("prefix", "test"));
  101.             namespaces.Add(new NSPrefixMapping("prefix2", "testNS2"));
  102.  
  103.             List<XPathValuePair> sampleXPathsForThisEBO = new List<XPathValuePair>();
  104.             sampleXPathsForThisEBO.Add(new XPathValuePair("Fir2Cost", "/prefix:main/prefix2:items/prefix2:item[prefix2:name='Fir2']/prefix2:cost", ""));
  105.             sampleXPathsForThisEBO.Add(new XPathValuePair("A", "/prefix:main/prefix2:a", ""));
  106.  
  107.             List<XPathValuePair> lookedupValues = makeNameValueStructureFromXMLStringForOutsystems(exampleNestedXML, sampleXPathsForThisEBO, namespaces);
  108.  
  109.             Console.WriteLine("Values read from XML:");
  110.             foreach (XPathValuePair xpathValuePair in lookedupValues) {
  111.               Console.WriteLine(xpathValuePair.ToString());
  112.             }
  113.  
  114.             Console.WriteLine("******************************************");
  115.  
  116.  
  117.             // Assume we want to change the data
  118.             List<XPathValuePair> newValues = new List<XPathValuePair>();
  119.             newValues.Add(new XPathValuePair(lookedupValues.ElementAt(0).Name, lookedupValues.ElementAt(0).XPath, "999"));
  120.             newValues.Add(new XPathValuePair(lookedupValues.ElementAt(1).Name, lookedupValues.ElementAt(1).XPath, "Altered ValueXXX"));
  121.  
  122.             Console.WriteLine("User changed values to:");
  123.             foreach (XPathValuePair xpathValuePair in newValues) {
  124.               Console.WriteLine(xpathValuePair.ToString());
  125.             }
  126.  
  127.             Console.WriteLine("Altered XML\n" + makeNewXMLWithNewValues(exampleNestedXML, newValues, namespaces));
  128.  
  129.  
  130.             // testXML(exampleXML, "/prefix:main/prefix2:b");
  131.             // testXML(exampleNestedXML, "/prefix:main/prefix2:items/prefix2:item[prefix2:name='Fir2']/prefix2:cost");
  132.  
  133.         }
  134.     }

Run it with:

  1. dotnet run

This is the HelloWorld.csproj file:

  1. <Project Sdk="Microsoft.NET.Sdk">
  2.  
  3.   <PropertyGroup>
  4.     <OutputType>Exe</OutputType>
  5.     <TargetFramework>netcoreapp2.2</TargetFramework>
  6.   </PropertyGroup>
  7.  
  8.   <ItemGroup>
  9.     <PackageReference Include="System.Xml.XDocument" Version="4.3.0" />
  10.   </ItemGroup>
  11. </Project>

Tags

RJM Article Type
Work Notes