Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merging xml from file into another xml file

Status
Not open for further replies.

DeanORenO

Programmer
Nov 12, 2010
2
US
The code that I have merges one xml file into another but it appends the xml to the end of the file, I need to insert the tag set in one file into another file in a particular location.

Here is my code:

protected void AddFilter(string ReportFile, string mdsName)
{
string FilterText = String.Empty;
string txt;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(ReportFile);

try
{
if (FilterTxt.Value.ToString() !="")
{
FilterText = HttpUtility.UrlDecode(FilterTxt.Value.ToString());
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
TextWriter tw = new StreamWriter("D:\\FilterTest.rdl");
tw.WriteLine(FilterText);
tw.Close();
XmlDocument doc = new XmlDocument();

XmlNode cont = doc.CreateElement("Filters");
doc.AppendChild(cont);
doc.

// first document to merge (the ReportFile)
XmlDocument reportrdl = new XmlDocument();
reportrdl.Load(ReportFile);
XmlNode imported = doc.ImportNode(reportrdl.DocumentElement, true);
doc.DocumentElement.AppendChild(imported);

String query = "DataSet[@Name="+mdsName+"]";
XmlNodeList mydataset = doc.SelectNodes(query);

foreach (XmlElement dst in mydataset)
{
// second document to merge (the new Filter File)
XmlDocument filterrdl = new XmlDocument();
filterrdl.Load("D:\\FilterTest.rdl");
imported = doc.ImportNode(filterrdl.DocumentElement, true);
doc.DocumentElement.AppendChild(imported);
}
// print merged documents
doc.Save("D:\\NewFilter.rdl");
}
}
finally
{
//fileStream.Close();
}

The attempt to use SelectNodes fails and it never drops into the foreach.

I need help kinda fast...

Thanks in advance

Dean


}//end AddFilter module
 
>I need help kinda fast...
Ok, just trying... From the look of the code, it seems doc has the root "Filters", hence, a query of this:

>String query = "DataSet[@Name="+mdsName+"]";
>XmlNodeList mydataset = doc.SelectNodes(query);

would most probably return null.

Try this instead for a quick probe see it does something positive, with descendant axis specified and also that you need additional pair of single-quotes?

[tt]String query = "[red]//[/red]DataSet[@Name=[highlight]'[/highlight]"+mdsName+"[highlight]'[/highlight]]";[/tt]
 
The imported node is being inserted but it ends up like this:

<Filters xmlns="">

I need to get rid of this attribute either after or from the AppendChild method somehow.
 
So effectively you mean to say your String query line is right and I said it is wrong. Well, I have nothing more to add to your need of quick help... except Filers does not belong to any namespace that's what the result is saying.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top