Select InnerText of XML is decoding URL - c#

My issue is, when selecting a node from xml file (packet in my case) is decoding an url that is the inner text of the node within the xml. Example
<url>"http://my.xml.org/?id=AAA%2DDDD%3dNNNLKLKJLKL%2"</url>
using an instance of XmlDocument:
xmlDoc.SelectSingleNode("url").InnerText;
The string that is returned is: "http://my.xml.org/?id=AAA/DDDD/NNNLKLKJLKL/"
How do I prevent this from happening? Notice that the %2 and %3's have been changed to forward slashes.

The code below gets correct results. What are you doing differently?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<url>\"http://my.xml.org/?id=AAA%2DDDD%3dNNNLKLKJLKL%2\"</url>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string results = xmlDoc.SelectSingleNode("url").InnerText;
}
}
}
​

Related

Altering string variable storing xml in c#

string content = ....
I have some XML content stored in a string as shown in the variable above. The content stored is similar to below ,
<?xml version="1.0"?>
<model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<ItemModel xsi:type="TypeKeep">
<Name>Name01</Name>
</ItemModel>
<ItemModel xsi:type="TypeDelete">
<Name>Name02</Name>
</ItemModel>
<ItemModel xsi:type="TypeDelete">
<Name>Name03</Name>
</ItemModel>
</Items>
</model>
In here I want to remove all the elements which have the type="TypeDelete" . In the sense I am trying to alter the content variable by removing the elments of TypeDelete
Any idea how I can achieve this ?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
XNamespace xsiNs = doc.Root.GetNamespaceOfPrefix("xsi");
List<XElement> removeNodes = doc.Descendants("ItemModel").Where(x => (string)x.Attribute(xsiNs + "type") == "TypeDelete").ToList();
for (int i = removeNodes.Count - 1; i >= 0; i--)
{
removeNodes[i].Remove();
}
}
}
}
If you want to remove a specific XML node from an XML file directly you can go through this link and try this

Why XML node value not changes?

I have this XML:
<?xml version="1.0" encoding="UTF-8"?>
<LayerDefinition version="1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LayerDefinition-1.0.0.xsd">
<VectorLayerDefinition>
<ResourceId>ddddd</ResourceId>
<FeatureName>SHP_Schema:HydrographicPolygons</FeatureName>
<FeatureNameType>FeatureClass</FeatureNameType>
<Geometry>SHPGEOM</Geometry>
<VectorScaleRange>
<AreaTypeStyle>
<AreaRule>
<LegendLabel/>
<AreaSymbolization2D>
<Fill>
<FillPattern>Solid</FillPattern>
<ForegroundColor>FFABC7E9</ForegroundColor>
<BackgroundColor>FF000000</BackgroundColor>
</Fill>
<Stroke>
<LineStyle>Solid</LineStyle>
<Thickness>0</Thickness>
<Color>FFABC7E9</Color>
<Unit>Inches</Unit>
</Stroke>
</AreaSymbolization2D>
</AreaRule>
</AreaTypeStyle>
</VectorScaleRange>
</VectorLayerDefinition>
</LayerDefinition>
I need to change this element:
<BackgroundColor>FF000000</BackgroundColor>
To this:
<BackgroundColor>FFFFAAAA</BackgroundColor>
Here is the way I try to do it:
XmlDocument doc = new XmlDocument();
doc.LoadXml(layoutXml);
XmlNodeList objNodeList = doc.SelectNodes("VectorLayerDefinition/VectorScaleRange/BackgroundColor");
objNodeList.InnerXml = "FFFFAAAA";
But the code above dosent works.What I do wrong here why the attitude not works?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication75
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XNamespace ns = doc.Root.GetDefaultNamespace();
XElement backgrounColor = doc.Descendants(ns + "BackgroundColor").FirstOrDefault();
backgrounColor.SetValue("FFFFAAAA");
}
}
}
I don't think you are going far enough down in your nodes. I think you need something more like this.
XmlNodeList objNodeList = doc.SelectNodes("VectorLayerDefinition/VectorScaleRange/AreaTypeStyle/AreaRule/AreaSymbolization2D/Fill");
objNodeList.selectSingleNode("BackgroundColor").innerXml= "FFFFAAAA";
Other wise your are trying to get a node inside VectorScaleRange that does not exist. Also you need the selectSingleNode() fucntion to grab the BackGroundColor node out of the list of nodes inside Fill.
Edited with the answer to your problem
XmlDocument doc = new XmlDocument();
doc.Load("texto.xml");
XmlNodeList objNodeList = doc.SelectNodes("/LayerDefinition/VectorLayerDefinition/VectorScaleRange/AreaTypeStyle/AreaRule/AreaSymbolization2D/Fill");
objNodeList.Item(0).SelectSingleNode("BackgroundColor").InnerXml = "FFFFAAAA";
doc.Save("texto.xml");

How to get the version number from a given XSLT file .

Say I have an XSLT file like below:
`<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-
prefixes="xs math"
version="3.0">`
....... and so on.
I need the output as 3.0 because the above file has version="3.0". I want to use C# to get this given the XSLT is in string format
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
string version = (string)doc.Root.Attribute("version");
}
}
}
Use XElement.Parse(yourString).Attribute("version").Value, where you add using System.Xml.Linq;. See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview for details of the used API.

XML file to other schema based XML file using c#

is it possible to rewrite XML file according to other schema XSD using c# ?
this is XML file
this is the current schema XSD file
and this is the new schema same everything but changed node names
so how to get a new XML from the old one based on the new XSD using c#?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication13
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> shipTo = doc.Descendants("shipto").ToList();
foreach (XElement ship in shipTo)
{
ship.Element("name").ReplaceWith(new XElement("FullName", (string)ship.Element("name")));
ship.Element("address").ReplaceWith(new XElement("FirstAddress", (string)ship.Element("address")));
ship.Element("city").ReplaceWith(new XElement("homeTown", (string)ship.Element("city")));
ship.Element("country").ReplaceWith(new XElement("HomeLand", (string)ship.Element("country")));
}
}
}
}

How to read XML document from property in Episerver

I want to read XML document from a property which is created in edit mode of Episerver.
I have made one property of type 'URL to Document'.
When I try to fetch it from code behind, it gives only file path. I am not able to read the content of XML file which is uploaded in property.
string XMLContent = Currentpage.Getproperty<string>("XMLFile");
Can anyone help out on this?
You need to load the file as well. Something like this:
var path = CurrentPage["XMLFile"] as string;
if (HostingEnvironment.VirtualPathProvider.FileExists(path))
{
var file = HostingEnvironment.VirtualPathProvider.GetFile(path) as UnifiedFile;
if (file != null)
{
using (var stream = file.Open())
{
// Here is your XML document
var xml = XDocument.Load(stream);
}
}
}
You can also load the file content by using the local path on disk, file.LocalPath.
try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string XMLContent = "";
//using XML
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(XMLContent);
//using xml linq
XDocument doc2 = XDocument.Parse(XMLContent);
}
}
}
​

Categories