How can I extract the data for a specific XML element and convert it into byte array
My XML file;

<?xml version="1.0" encoding="utf-8" ?> 
<root> 
<creditcard> 
<number>19834209</number> 
<expiry>02/02/2002</expiry> 
</creditcard> 
<name> 
<first>Mary</first> 
<mi>V</mi> 
<last>Jones</last> 
</name> 
<personal> 
<dob>01011966</dob> 
<gender>male</gender> 
</personal> 
</root>

I want to extract the element <creditcard> and all nodes only within the <creditcard> element and convert it into ByteArray?
Can someone pl. show me the example.

Hi,

Please read into this tutorial. Try to adapt the examples to your problem. Start writing your solution and if you are blocked somewhere reply here with your specific question and with code fragment.

Best regards,
Tamas

kk i help a little.

XmlDocument xdoc;
xdoc = new XmlDocument();
xdoc.Load("yourxmlfile.xml");
XmlNodeList creditcards = xdoc.GetElementsByTagName("creditcard");
string s = creditcards[0].InnerXml.ToString() ;

if you want the plain data use .InnerText

Thanks. I had to change my requirement a little. Here is my test.xml file:

<root>
 <EncryptedData Id="EncryptedElement1" Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
  <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /> 
 <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
 <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
  <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> 
 <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
  <KeyName><RSAKeyValue><Modulus>s24s1m6t2JIFbrHo06SdPoSoi7FTkvJhE0fNJVMooKTt6lnp4MoJn9Fcu1JWz7v9Q+jMXrtlc6lETIM6NJ+8Gu5TcZpX4UUy7V6iTpN5BEbsivwEqB12hV/AvyJhaNd2dVT5iWoYXonrPIGITLcqGz1B+kBkgsS1EawnlVdv1SU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyName> 
  </KeyInfo>
 <CipherData>
  <CipherValue>FnCyp9m3g9JfvMx+y5ZGYFLJOuf9fCCbkJ1ADJajNRiR2RJOOy6cWDi9e/4/fpVaoaCxgaZvLD3pyG5fguzzO+Z8NQNMtBx5R3utvWFoWYS6le9AiWIsg6PwpIgYqHjFqNgMDE6lhJgc8rwBDnJJESg0LiM4HFwHJZ6P/sEWj7M=</CipherValue> 
  </CipherData>
 <ReferenceList>
  <DataReference URI="#EncryptedElement1" /> 
  </ReferenceList>
  </EncryptedKey>
  </KeyInfo>
 <CipherData>
  <CipherValue>M9xlga622FM56oxXPJuhMUJnzjIrifnSIaGDFO8fkFeSTGuRSYEIcWcaYlRFMSUpqwpWqd8DZOBXy37Atj92k8pRLR82W4Cl0odgMFROmBmUymsWGvdi14QrqSPfK3A/9UFbLMhhO3MXXzVCm9Irbg==</CipherValue> 
  </CipherData>
  </EncryptedData>
 <name>
  <first>Mary</first> 
  <mi>V</mi> 
  <last>Jones</last> 
  </name>
</root>

I want to access the element with Id="EncryptedElement1"
Here is my code.

XmlDocument xDoc = new XmlDocument(); 
            xDoc.Load("test.xml");
            XmlElement decryptElement = xDoc.GetElementById("EncryptedElement1");
            string s = decryptElement.InnerXml.ToString();

The GetElementById returns null, even though the element with Id EncryptedElement1 exists. Can you tell me what is wrong?

Ok let's see what MSDN says:

The DOM implementation must have information which defines which attributes are of type ID. Although attributes of type ID can be defined in either XSD schemas or DTDs, this version of the product only supports those defined in DTDs. Attributes with the name "ID" are not of type ID unless so defined in the DTD. Implementations where it is unknown whether the attributes are of type ID are expected to return null.

Thanks. I have to put in DTD specific syntax to make this work. But I cannot modify this file as I get it by web service. So I was searching for other ways to get the value. Is there a way I can do this with Linq to XML?

This article has been dead for over six months. Start a new discussion instead.