Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

recursiv search in a xml file

Posted on 2013-01-19
6
Medium Priority
?
374 Views
Last Modified: 2013-02-08
Hi Everybody,

i'm looking for a solution to read an xml file recursiv.  (see attached xml file).
What i have is the "Filename", Filename = 193478618937468178.000.
Beginning from the the "Filename" i need the ID of the next node on the top.
From this ID i want to go to the next Childnode on the top, and so on.

Is there any idea how could i do this with Xpath and C# .net?

Sample:
Filename = 193478618937468178.000 --> goto ID = 2657
From
ID = 2657 --> goto ID = 1392
From
ID = 1392 --> goto ID = 8345
From
ID = 8345 --> goto ID = 1242
From
ID = 1242 --> goto ID = 39



Thank you in advance
Regards Bounty
xpath-Variant3.xml
0
Comment
Question by:bounty457
  • 3
  • 3
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38796780
The most efficient way to do this, I think is by running a simple XSLT from C# on the document
You can pass in the file name as a parameter

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:param name="fname">193478618937468178.000</xsl:param>
    <xsl:key name="object" match="Object" use="Fields/Field[@name='Filename']"/>
    
    <xsl:template match="/">
        <xsl:variable name="root-node" select="key('object', $fname)"/>
        <xsl:text>Filename = </xsl:text>
        <xsl:value-of select="$fname"/>
        <xsl:text> --> goto ID = </xsl:text>
        <xsl:apply-templates select="$root-node"/>       
    </xsl:template>
    
    <xsl:template match="*[@id]">
        <xsl:value-of select="@id"/>
        <xsl:if test="ancestor::*[@id]">
            <xsl:text>&#10;From&#10;ID = </xsl:text>
            <xsl:value-of select="@id"/>
            <xsl:text> --> goto ID = </xsl:text>
            <xsl:apply-templates select="ancestor::*[@id][1]"></xsl:apply-templates>
        </xsl:if>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Comment

by:bounty457
ID: 38796829
Thank you for the answer.

Could you please short explain how must i use the code from C#?
i want do write the data for each "filename" a one datatable after parsing the xml.

Regards Bountry
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1600 total points
ID: 38796874
Here is an example

http://msdn.microsoft.com/en-us/library/dfktf882%28v=vs.85%29.aspx

xslt.Load("discount.xsl");
put the name of your XSLT file there

drop the two DateTime lines

      argList.AddParam("discount", "", discountDate.ToString());
should be
      argList.AddParam("fname", "", "193478618937468178.000");
you can put in there any value you want

 XmlWriter writer = XmlWriter.Create("orderOut.xml");
is where you want the result to be

xslt.Transform(new XPathDocument("order.xml"), argList, writer);
pull in the right file
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:bounty457
ID: 38797985
Hi,

the sample from MSDN work right. If i change the .xsl and .xml File
 
i get the message:
"Leerzeichen können aus bereits geladenen Eingabedokumenten nicht entfernt werden. Stellen Sie das Eingabedokument stattdessen als XmlReader bereit."
Translate:
"Use XMLReader - Empty Spaces can't delete from the source file"

I looked at MSDN:
http://msdn.microsoft.com/en-us/library/241y3z4e.aspx
I use the  XML Reader with the following code:
// Create the XslCompiledTransform and load the style sheet.
      XslCompiledTransform xslt = new XslCompiledTransform();
      string stylesheet = "Variant3.xsl";

//Create the reader to load the stylesheet.  
//Move the reader to the xsl:stylesheet node.
          XmlTextReader reader = new XmlTextReader(stylesheet);
          reader.Read();
          reader.Read();
          xslt.Load(reader);
// Create the XsltArgumentList.
          XsltArgumentList argList = new XsltArgumentList();
// Add my first Filename to the Arglist
          argList.AddParam("fname", "", "193478618937468178.000");
// Create an XmlWriter to write the output.             
         XmlWriter writer = XmlWriter.Create("Variant3_Out.xml");
// Transform the file.
         xslt.Transform(new XPathDocument("xpath_Variant3.xml"), argList, writer); 
        writer.Close();

Open in new window

Now i get the Message:

Das Stylesheet muss entweder mit einem 'xsl:stylesheet'- oder einem 'xsl:transform'-Element oder aber mit einem Literalergebniselement beginnen, welches das 'xsl:version'-Attribut aufweist. Dabei gibt das Präfix 'xsl' den Namespace 'http://www.w3.org/1999/XSL/Transform' an.
Translate:
"The Stylesheet must start with: xsl:stylesheet or xsl:transform element or an literal with 'xsl:version'-Attribut. The präfix must have the namespace http://www.w3.org/1999/XSL/Transform'
What must i add to the XSL File?

Regards Bounty
Variant3.xsl
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38798010
apparently you have not loaded the XSLT correctly, since the above code I posted has all it needs
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

Can you check that it finds the XSLT file?
0
 
LVL 1

Author Comment

by:bounty457
ID: 38798033
Yes the File is found. The path is correct. It is a load Exception of the .xls File. It is working on your code?
I change the Variant3.xsl how you wrote.
I attached the screenshot from the error message.
Load-Exception.PNG
Variant3.xsl
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question