Encrypting XML Element - asp.net

I have created XML file called users.xml
Looks like this:
<Users>
<user>
<uin>"0012345"</uin>
<name>black</name>
<email>"bk#hotemail.com"</email>
<created>"3/02/2010"</created>
</user>
<user>
<uin>"123456780"</uin>
<name>sam</name>
<email>"sam#hotmail.com"</email>
<created>"3/02/2010"</created>
</user>
<user>
<uin>"123456799"</uin>
<name>kblack</name>
<email>"kblack#hotmail.com"</email>
<created>"3/02/2010"</created>
</user>
</Users>
I want to encrypt the element. Using code like
XmlElement uinelement = (XmlElement)xmldoc.SelectSingleNode("Users/user/uin");
...encrypts only first UIN from the user.xml file.
How can I Encrypt all UIN elements?
Thank you
Kanta

It looks like your xml was not correctly encoded, so I can't see your document structure, however I would guess that using the SelectNodes() method of the XmlDocument class instead of SelectSingleNode() would do the trick.
You may also want to look at some of the Linq to XML capabilities if you need to output the transformation to a new XML doc.

Related

Create XML doc with Linq to XML

I have an ASP .Net site where i am attempting to create a page to allow the user to create an XML document. New with XML so I'm not sure if im missing something here but all i am trying to do is create an XML document using Linq to XML.
I saw this example as part of my research
Dim xmlDoc As XDocument = _
<?xml version="1.0" encoding="utf-16"?>
<!-- This is comment by you -->
<Employees>
<Employee id="EMP001">
<name>Wriju</name>
<![CDATA[ ~~~~~~~...~~~~~~~~]]>
</Employee>
</Employees>
xmlDoc.Save("somepath\somefile.xml")
but i just dont seem to be able to get it working in VS 2010 or VS 2013. As soon as i enter <?xml and enter to the next line i just get errors that the syntax is incorrect. Reading further i have seen some C# examples but they dont seem to be "readable" in VB .Net.
I've tried using online converters but again no luck in that either. Could anyone guide me or point me to the right direction to create an XML document with elements etc using Linq to XML? I think i can use XDocument but i'm a little confused if that would allow me to use Linq to XML or if this is an old concept.
Thanks
Use this
Dim xmlDoc As XDocument = <?xml version="1.0" encoding="utf-16"?>
<!-- This is comment by you -->
<Employees>
<Employee id="EMP001">
<name>Wriju</name>
<![CDATA[ ~~~~~~~...~~~~~~~~]]>
</Employee>
</Employees>
'it assume that /youfolder is in the root and it has read and write permission
xmlDoc.Save(Server.MapPath("/yourfolder/somefile.xml"))

How to retrieve XML data using XPath Expression

I have a XMLDataSource somewhat like:
<bookstore>
<author>author1</author>
<publication>publication1</publication>
<book>
<genre>Thriller</genre>
<name>ABC</name>
</book>
<book>
<genre>Romance</genre>
<name>XYZ</name>
</book>
<book>
<genre>Horror</genre>
<name>000</name>
</book>
</bookstore>
I am storing these in a asp:formview. I am able to store author and publication values but not sure how can I store the value of book/name based on some condition? Actually I just want to use condition that I need to store the value of "name" if "genere=Romance". something like this. I tried using XPath expression bookstore/book/genre[. ='Romance'] but not sure how to access the value of tag. Checked the following resource:
http://msdn.microsoft.com/en-us/library/ms256086.aspx
Appreciate if someone can help me.
I tried using XPath expression
bookstore/book/genre[. ='Romance']
but not sure how to access the value
of tag
Almost. This XPath expression:
/bookstore/book[genre='Romance']/name
String value: XYZ
You probably need to add /text() to get the contents of the XML tag instead of just the tag. There is a great XML cheat-sheet here that should help you.

how to convert an XML into an array in asp.net

i need to convert an XML into an array using asp.net.
I tried searching on the web but couldn't find the proper solution.
Can someone please help me with this
Thanks,
Alloi
I find LinqToXML helpful in these circumstances. I have a very simple XML document:
<?xml version="1.0" encoding="utf-8" ?>
<People>
<Person>
<id>1</id>
<name>Dave</name>
</Person>
<Person>
<id>2</id>
<name>Pete</name>
</Person>
<Person>
<id>3</id>
<name>Marie</name>
</Person>
</People>
And the following code that will turn it into an array of objects:
FileStream file = new FileStream("data.xml", FileMode.Open);
XDocument xmldoc = XDocument.Load(file);
var people = (from p in xmldoc.Descendants("Person")
select new
{
ID = p.Element("id").Value,
Name = p.Element("name").Value
}).ToArray();
foreach (var person in people)
{
Console.WriteLine(person.Name);
}
I think LinqToXML might be helpful in your situation. Here's a sample in vb.net (I could not find anything in csharp...): http://msdn.microsoft.com/en-us/vbasic/bb738047.aspx#convnodesarray
If you have an XSD to which the XML refers to, that probably can be a starting point on the implementation of array (or list).
A way... might help you
Read xml into dataset.
Iterate dataset on the basis of table rows and add each row entity to a list.
display list as array or list itself.
I don't know if there is a .net class that performs this action directly or not but in case there is no you have 2 options :
1 . Load XML into dataset then loop in the dataset and set elements of the array.
2 . Get XML nodes number then loop through XML and read each node element then put it in the array.
Hope that this is useful
Alloi - i think that due to the complexity inherent in xml structures, this will very much depend on the xml itself. what i'm saying is that a generic solution may be very difficult to make bomb proof, whereas a solution to a 'known' structure may be fairly straightfwd. the serialiser may allow you to save it to ToList() or similar but beyond that, it's a case of horses for sources i think. :)
jim
[edit] - link that may be useful: http://www.c-sharpcorner.com/UploadFile/chauhan_sonu57/SerializingObjects07202006065806AM/SerializingObjects.aspx
also - a nice little link showing how to convert xml->json in c# : http://www.phdcc.com/xml2json.htm

issue with a xmlns

I have an xml file and an xslt file.
The xml file has a xmlns="exa:com.test" attribute.
If i remove this attribute in the xml the xpath sentences in my xslt works. But if i leave it, doesnt work.
Im using the following code to mix xml and xslt:
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(HttpContext.Current.Server.MapPath("xslt\\searchresults.xslt"));
transformer.Transform(xmlreader, null, utf8stringwriter);
What im doing wrong? How could i mix xml and xslt if the xml has the xmlns attribute on top?
The xmlns attribute without prefix name replaces the default namespace, so that your query matches nodes with a different (empty) namespace. You need to use a prefixed namespace in the XSLT XPath queries (or a XmlNamespaceManager for isolated XPath queries) and your queries will work again as expected.
In the XSLT:
<xsl:stylesheet ... xmlns:exa="exa:com.test">
Then, assuming that you used to match for instance xyz, you now change your query like this:
<xsl:template match="exa:xyz"> ...
In general you might want to read some docs on XML namespaces.
It should be noted though, though the namespace string is to just be seen as an exact literal string that is used as an identifier with no other meaning. The string must conform to an IANA uri, this is to guarantee uniqueness of namespaces. Some parsers refuse to parse namespaces that do not conform.
Also note that because they are strings http://www.w3.org/1999/xhtml and for instance http://w3.org/1999/xhtml/ are for the purposes of namespaces completely different namespaces.
You might want to read up on how namespaces in XML work. exa:com.test is as far as I know not a valid namespace string.

XQuery - remove nodes based on its sub element being in the "ban" list

I am a total noob with XQuery, but before at start digging deep into it, i'd like to ask some experts advice about whether i am looking at the correct direction.
The problem:
A huge xml file which contains a whole lot of users and their access information (password access rights and so on) example below:
<user>
<name>JC1234</name>
<password>popstar</password>
<accesslevel>0</accesslevel>
</user>
<user>
<name>AHkl</name>
<password>Rudy10!</password>
<accesslevel>2</accesslevel>
</user>
i have a list of user names (csv file) that i need to remove from that huge xml files.
the result should be a new xml file wihtout those removed users....
is this feasable with XQuery?
any advice for a quick and dirty solution is welcomed!
There is no standard way of loading a CSV file in vanilla XQuery 1.0, although most implementations have an unparsed-text function or similar. If not the contents of the file can be passed in as a parameter.
The CSV file can be parsed using the tokenize function:
declare variable $names = tokenize(unparsed-text("banned.csv"), ",")
And the actual query is quite straightforward. Assuming your document is a a fragment containing just a list of <user /> nodes then the query is simply
doc("users.xml")/user[not(name=$names)]
If however the XML file contains a lot of other data then you may find XSLT's templating facilities more useful.

Resources