Decrypt a string contained in XML before output C# - c#

Ok, I have an xml document which is returned via a c# function and output via XSLT. In the XML document there is an encrypted string which I need to decrypt before output.
Basically, i need to run "encryptedstring" through a decrypting function in order get the real value.
XML Structure:
<root>
<order encryptedstring="676696969hjhig">
</root>
Outputting function:
XmlDocument xmlOrders = Data_Functions.Get_All_Orders();
xmlOutput.DocumentContent = xmlOrders.OuterXml;
I am assuming i need to loop through the XML document, get the value for each "encryptedstring", run that value through the decrypt function and re-inject it back into the xml document but am unsure of best way to go about that.
The decryption has to be done in the codebehind in c# by passing a string through decryptString();

With a bit of guidance from the question suggestion by #momar this was solved in the following way...
XmlNodeList aNodes = xmlOrders.SelectNodes("//root/order");
foreach (XmlNode aNode in aNodes)
{
XmlAttribute ccAttribute = aNode.Attributes["encryptedstring"];
string attValue= ccAttribute.Value;
string encKey = ConfigurationManager.AppSettings["EncryptionKey"];
string decrypt = Crypto.DecryptStringAES(attValue, encKey);
ccAttribute.Value = deencrypt;
}
xmlOutput.DocumentContent = xmlOrders.OuterXml;

Related

read remote xml from server using c#

Hello i am trying to get simple xml file from server and to read the data so i can convert it to list. I was try few lib and code with no success so far. I am getting the xml content in one line without any tags <> and the count is always 0 zero. The XML string. I need to get the data that inside camp tag
<campaigns>
<mainPage>http://example.com</mainPage>
<orderPage>https://www.example.co.il/adver/</orderPage>
<totalCount>3</totalCount>
<onLineCount>2</onLineCount>
<campaignList>
<camp id="557">
<name>test1</name>
<status>on</status>
<rating>5</rating>
<url>http://example.com/557</url>
</camp>
<camp id="559">
<name>test1</name>
<status>on</status>
<rating>5</rating>
<url>http://example.com/559</url>
</camp>
<camp id="660">
<name>test1</name>
<status>off</status>
<rating>5</rating>
<url>http://example.com/660</url>
</camp>
</campaignList>
And the c# code i am trying so far
XElement xelement = XElement.Load("http://example.com/test.xml");
var name = from nm in xelement.Elements("camp")
where (string)nm.Element("status") == "on"
select nm;
Response.Write(name.Count());
foreach (XElement xEle in name)
{
Response.Write(xEle);
}
XElement.Elements() means search in children tags. I think what you need is Descendants() or xelement.Element("campaigns").Element("campaignList").Elements("camp")

Get Xml content into a string from a subchild using c#

It is my first time posting, and I am very new to C#, so I appreciate the help. I have so many variations of this code that are close to what I need, but I can't get on my own.
I am trying to get the contents of a sub-child in an XML sting using c#. This is the sting I receive:
<?xml version="1.0" encoding="UTF-8"?>
<activation>
<activationOutput>
<AID>7706a84f-5sef5-4b49-891c-98414ebb61d5</AID>
<protectionKeyId>18305465463798407</protectionKeyId>
<activationString>encoded xml string</activationString>// What I need
</activationOutput>
<activationInput>
<activationAttribute>
<attributeName></attributeName>
<attributeValue></attributeValue>
<isvPermission></isvPermission>
<endUserPermission></endUserPermission>
</activationAttribute>
<activationAttribute>
<attributeName></attributeName>
<attributeValue></attributeValue>
</activationAttribute>
<comments></comments>
</activationInput>
</activation>
I need to get the activationString value out into a string. Having the other elements would be nice, but I only care about getting into a string v2c called. The information in between the tags is another encoded XML string.
I first bring the sting into an XML element using:
XElement rootActivation = XElement.Parse(encodedActivationResponse, LoadOptions.SetLineInfo);
What has got me stumped is how to get the sub child out as a string. Using XElement element gets me the values for all of the children under ActivationOutput, but I only need the last one.
string activationOutput = rootActivation.Element("activationOutput").Value;
It sends me a long unusable string
7706a84f-5sef5-4b49-891c-98414ebb61d518305465463798407encodedxmlstring
I've also tried:
IEnumerable<XElement> activationString = rootActivation.Elements("activationString");
It looked promising, but I don't know how to get the value out as a string. It shows as
{System.Xml.Linq.Extenstions.GetDescendantsXElement>}
I was hoping that I could take the variable and push it into a string using:
string v2c = activationString.ToString;
But that does not work either
Your attempt
string activationOutput = rootActivation.Element("activationOutput").Value;
does give you a concatenation of all the text() nodes that are children of <activationOutput>. Obviously, that's not what you want.
To solely get the <activationString>'s text() node use
string activation =
(string)rootActivation.Element("activationOutput").Element("activationString").Value;
One restriction of this approach is: it only gets the first of each elements.
If that is the only element called activationString you could just do
string activationString = XDocument.Parse("xmlFilePathHere").Descendents("activationString").FirstOrDefault().Value;
In VB.Net
Dim rootActivation As XElement
'for testing
rootActivation = <activation>
<activationOutput>
<AID>7706a84f-5sef5-4b49-891c-98414ebb61d5</AID>
<protectionKeyId>18305465463798407</protectionKeyId>
<activationString>encoded xml string</activationString>
</activationOutput>
<activationInput>
<activationAttribute>
<attributeName></attributeName>
<attributeValue></attributeValue>
<isvPermission></isvPermission>
<endUserPermission></endUserPermission>
</activationAttribute>
<activationAttribute>
<attributeName></attributeName>
<attributeValue></attributeValue>
</activationAttribute>
<comments></comments>
</activationInput>
</activation>
Dim activationString As String = rootActivation.<activationOutput>.<activationString>.Value

How get JSON string from XML string using C#?

I am trying to convert XML string to C# object, I have json string acle in xml tag, as shown below,
<message> <data:gcm xmlns:data=\"google:mobile:data\">{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}</data:gcm> </message>
I want json string from data tag I just want this string from above xml,
{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}
So how can I get this using c#.?
Thank you in advance.
By reading some LINQ to XML documents I got the solution which is like below,
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(msg.ToString());
var result = xdoc.Element("message").Descendants();
var myString = result.FirstOrDefault().Value; //This will out given json string
Again Thank you #JonSkeet for your suggestion.!

I want to create an array containing my xmldocument nodes in c#

Hello I have created an xml file with motivational quotations, and I want to read those quotations into an array.
Here is what my xml file looks like:
<?xml version="1.0" encoding="utf-8" ?>
<MotivationalQuotes>
<quotation>
<quote>Life is about making an impact, not making an income</quote>
<author>Kevin Kruse</author>
</quotation>
<quotation>
<quote>Whatever the mind of man can conceive and believe, it can achieve</quote>
<author>Napoleon Hill</author>
</quotation>
</MotivationalQuotes>
I am trying to store each individual quotation (without the author) into an array, so far I have the below code working - which creates a message box and iterates through the xml file displaying the text from each quotation.
1) How can I modify this code to create a string array, where each item in the array is a quotation (i.e. each item in the array is the contents that is currently being displayed to the messagebox in my foreach loop?
2) how do I return a random item from the array, once it is created?
3) As an extension to my question... my xml file only has motivational quotes in at the moment, but it will have more inspirational, funny etc... how can I specify to only include quotations into the array if they are inside the MotivationalQuotes tag.
Thanks for the help!
public void motivate()
{
XmlDocument doc = new XmlDocument();
doc.Load("quotations.xml");
XmlNode Node = doc.DocumentElement;
foreach (XmlNode Node1 in Node.ChildNodes)
{
MessageBox.Show(Node1.FirstChild.InnerText);
}
}
You should use XDocument and LINQ.
To get all quotes
using System.Xml.Linq;
var quotes = XDocument
.Load("quotations.xml")
.Descendants("quote")
.Select(q => q.Value)
.ToArray();

Navigating using XPath stored as a string

I am trying to store XPath of an XML attribute as a string in a separate file so that if XPath changes, I can easily modify the navigation to the attribute without changing code.
For example, in following XML:
<Result>
<Server = "main">
<Client id="1"></Client>
</Server>
</Result>
if I want to navigate to id attribute of Client element, I can have following string:
Result->Server->Client->id
I am not sure how in C# I can navigate using this string form of XPath and then, read the attribute value from the target XML.
Please help.
Harit
Well, firstly, your XML is a bit strange, with
<Server = "main">
Do you mean
<Server id="main">
But, regardless of that, you could just store the XPath directly instead of your string version. Like:
/Result/Server/Client[0]/#id
then you read the string from the file and pass it into something like:
public string GetClientIdString(string xPathString)
{
var doc = new XmlDocument();
doc.Load("SomeXml.xml");
return doc.DocumentElement.SelectSingleNode(xPathString).Value;
}
The issue becomes that you can't really store the XPath exactly how you would like if you plan on having more than one Client under Server. If you need that functionality, though, you could parse out your version of the XPath and do something like:
public IEnumerable<string> GetClientIdStrings(string elementXPath, string attribute)
{
var doc = new XmlDocument();
doc.Load(SomeXml.xml);
var clientIdStrings = new List<string>();
foreach(var node in doc.DocumentElement.SelectNodes(elementXPath))
{
clientIdStrings.Add(node.Attributes[attribute].Value);
}
return clientIdStrings;
}

Categories

Resources