Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi,
i have two strings like this:

string scope =
HTML
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + 
"<haspscope>" + 
"    <hasp type=\"HASP-HL\" />" + 
"</haspscope>";


string format =
HTML
"<haspformat root=\"hasp_info\">" + 
"    <feature>" + 
"       <attribute name=\"id\" />" + 
"       <attribute name=\"locked\" />" + 
"       <attribute name=\"expired\" />" + 
"       <attribute name=\"disabled\" />" + 
"       <attribute name=\"usable\" />" + 
"    </feature>" + 
"</haspformat>";


the called method receives these two string and returns an third string like this:

string result=
XML
<?xml version="1.0" encoding="UTF-8" ?>
<hasp_info>
  <feature id="0" locked="true" expired="false" disabled="false" usable="true" />
  <feature id="1" locked="true" expired="false" disabled="false" usable="true" />
</hasp_info>


How can i get the value of the second feature id (it's 1) ?

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 4-Feb-14 12:34pm    
What have you tried so far?
—SA

All XML parsers allow to parse XML stored in a string, via a MemoryString (if parsing from string is not directly implemented). This is my short overview of those parsers available in .NET FCL:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


For reading a string content from stream, use System.IO.MemoryStream:
http://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.110%29.aspx[^].

The code sample with putting a string to a memory stream is shown in the MSDN article referenced above. You only need to rewind the stream back to the first position before reading: memStream.Seek(0, SeekOrigin.Begin);. From this point, you can read XML from this stream.

—SA
 
Share this answer
 
one possible approach is to create System.Xml.XmlDocument object from string and then find the necessary node
C#
var xml = new System.Xml.XmlDocument() {InnerXml = result};
var nodes = xml.GetElementsByTagName("feature");
var node = nodes.Cast<XmlElement>().SingleOrDefault(e => e.GetAttribute("id") == "1");
if (node !=null)
{
   // do something, e.g. get node.Attributes
}
 
Share this answer
 
Option #1 using XmlDocument...
C#
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><hasp_info><feature id=\"0\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /><feature id=\"1\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /></hasp_info>";
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml(xml);
System.Xml.XmlNode node = xdoc.SelectSingleNode("//hasp_info/feature[@id = '1']");

// do something with the selected node

Option #2 using LINQ to XML...
C#
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><hasp_info><feature id=\"0\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /><feature id=\"1\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /></hasp_info>";
System.Xml.Linq.XElement doc = System.Xml.Linq.XElement.Parse(xml);
System.Xml.Linq.XElement element = (from el in doc.Elements()
                                                where el.Attribute("id").Value == "0"
                                                select el).FirstOrDefault<System.Xml.Linq.XElement>();
// do something with the selected element
 
Share this answer
 
v2
actually my c# code to read my output string is:

XmlReader reader = XmlReader.Create(new StringReader(result));
reader.MoveToElement();
reader.MoveToAttribute("id");
string firstId = reader.Value;
reader.MoveToAttribute("id");
string secondId = reader.Value;
Debug.WriteLine("first: "+firstId+"\t second: "+secondId);

but it doesn't works.
I need to get the value of the second attribute "id" and save it in a string.
(Example: i need to save value '1' in a string)

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900