 |
| General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category.
** PLEASE BE SPECIFIC WITH YOUR QUESTION **
When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 17th, 2005, 11:45 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
2 XML TO 1 XML
Hi,
I need some help and was wondering if someone has managed to read two xml documents into a .net class and then produced a third xml document. Basically, I want to transform two xml documents into one with nodes from both.
I would really appreciate if someone could produce a simple code sample to get me started or maybe point me to an online tutorial.
As you can see from the xml below, if the Survey ID is the same in both XML I need to output everything from the ALL SURVEY XML together with the <Options> and <AccessCode> nodes found only on the LookUP survey document.
Cheers,
Claudio
LOOKUP SURVEY WITH ACCESS CODES
===============================
<Surveys>
<Survey id="Eu02r1">
<Description>Europa Test 2002 (Oct00-Sept01) - +Global - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle />
<TotalWeight />
<TotalSample />
<RegionCodes />
<WeightLabel />
<WeightUnits />
<Language ID="">
<Description />
</Language>
<Options>
<SupportsAlphaSearch />
<Barnardisation />
<ReconcileUniverse />
<RFModelType />
</Options>
<AccessCodes>
<CODE>ACR1</CODE>
<CODE>ACR2</CODE>
<CODE>ACR3</CODE>
<CODE>APPL</CODE>
</AccessCodes>
</Survey>
<Survey id="Eu01">
<Description>Europa Test 2001 (Apr00Mar01) +Global pop</Description>
<Copyright>Test International 2001</Copyright>
<RegionCodes />
<TotalWeight />
<TotalSample />
<WeightLabel />
<WeightTitle />
<WeightUnits />
<Language ID="">
<Description />
</Language>
<Options>
<SupportsAlphaSearch />
<Barnardisation />
<ReconcileUniverse />
<RFModelType />
</Options>
<AccessCodes>
<CODE>@@@@</CODE>
</AccessCodes>
</Survey>
</Surveys>
ALL SURVEY XML
=================
<?xml version="1.0" encoding="UTF-8"?>
<Surveys>
<Survey id="bbs2000p">
<Description>BBS 2000 - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle>Pop</WeightTitle>
<TotalWeight>1000</TotalWeight>
<TotalSample>500</TotalSample>
<LanguageCount>0</LanguageCount>
</Survey>
<Survey id="Eu01">
<Description>Europa Test 2001 (Apr00-Mar01) - +Global - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle>Pop</WeightTitle>
<TotalWeight>1000</TotalWeight>
<TotalSample>500</TotalSample>
<LanguageCount>5</LanguageCount>
<Language id="ENG">
<Description>English (United States)</Description>
</Language>
<Language id="FRA">
<Description>French (France)</Description>
</Language>
<Language id="ITA">
<Description>Italian (Italy)</Description>
</Language>
</Survey>
<Survey id="Eu02r1">
<Description>Europa Test 2002 (Oct00-Sept01) - +Global - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle>Pop</WeightTitle>
<TotalWeight>1000</TotalWeight>
<TotalSample>500</TotalSample>
<LanguageCount>5</LanguageCount>
<Language id="ENG">
<Description>English (United States)</Description>
</Language>
<Language id="FRA">
<Description>French (France)</Description>
</Language>
<Language id="DEU">
<Description>German (Germany)</Description>
</Language>
<Language id="ITA">
<Description>Italian (Italy)</Description>
</Language>
<Language id="ESP">
<Description>Spanish (Traditional Sort)</Description>
</Language>
</Survey>
</Surveys>
|
|

February 17th, 2005, 01:39 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
without dealing with XML classes in .NET framework,I think there is a possibility,
you can use DataSet object and its ReadXmL method then make a DataRelation between two DataTables in your dataset and make another dataset and use WriteXml method for writing the results.
that was my opinion.
_____________
Mehdi.
software student.
|
|

February 18th, 2005, 02:24 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I think you can "merge" those 2 xml's programmatically into one xml. Then you could use that new xml string with an xslt to transform.
Best Regards
Vadivel
MVP ASP/ASP.NET
http://vadivel.thinkingms.com
|
|

February 18th, 2005, 06:43 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Mehdi, Vadivel
Thanks for your reply.
I do not want to use xslt to transform the xml documents. I want to use only .NET. So I think I would like to know how to use the dataset as mentioned by Mehdi.
I was wondering if you have any sample code for that.
I know how to create a dataset and populate it with one xml but you said I have to populate it with 2 xmls and then make a DataRelation between two DataTables in your dataset and make another dataset and use WriteXml method for writing the results.
Cheers,
Claudio
|
|

February 18th, 2005, 04:25 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
when you read your XML files you would have two DataTables in your DataSet then make two DataColumns as parent and child and then make a DataRelation and set its parent and child column then iterate through the DataRows of the parent table and get the equivalent DataRow(s) from the child DataTable and make new DataTable(using Clone method from the child DataTable) and add those rows to the result table and finally add those two columns(<Options> and <AccessCode>) to the result DataTable...
check out this for an example about making a DataRelation.
_____________
Mehdi.
software student.
|
|

February 20th, 2005, 01:16 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Mehdi,
Thanks a lot for your reply.
I was wondering if you could help me with the code to do that. I have started it but am stuck. This is what I have done:
On the Page_Load event:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XML\\allSurveys.xml"));
ds.ReadXml(Server.MapPath("XML\\UserXML.xml"));
DataView dv = ds.Tables[0].DefaultView;
dgDV.DataSource = dv;
dgDV.DataBind();
What you said makes lot of sense but since I am still learning how to use the dataset I do not know how to do it. Please, could you share you knowledge with us in the forum??
You said that I would have two DataTables in my DataSet then make two DataColumns as parent and child and then make a relationship. However, how can I see the two DataTables ???
I have tried to search our forum database for some sample code but could not find any to do something like that and would really be gratfull if you could also help with the code.
Cheers,
Claudio
|
|

February 20th, 2005, 05:15 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Claudio,
I looked in your XML files more carefully,in your case you have a XML file with nested tags and ... DataSet and DataRelation are just suitable for simple XML files not every complicated XML file,
I can't have any idea on this,try this code,
Code:
DataSet dsParent=new DataSet();
DataSet dsChild=new DataSet();
dsParent.ReadXml("XML\\allSurveys.xml");
dsChild.ReadXml("XML\\UserXML.xml");
foreach(DataRow dr in dsChild.Tables[0].Rows)
foreach(DataRow drnested in dsParent.Tables[0].Rows)
if(dr["id"].ToString()==drnested["id"].ToString())
dr.ItemArray=drnested.ItemArray;
dsChild.WriteXml("C:\\Result.xml");
I think you will finally have to look for XML classes and work on them because of your XML files.
_____________
Mehdi.
software student.
|
|

February 21st, 2005, 07:26 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Mehdi,
Thanks ever so much for you help.
I have tried your code but the result was not right.
The xml expected has to include all the nodes from the ALL SURVEY XML and only the <Options> and <AcessCodes> from the LOOKUP SURVEY WITH ACCESS CODES XML and only when the SurveyID match both XML files. This is the result I would like:
<?xml version="1.0" encoding="UTF-8"?>
<Surveys>
<Survey id="Eu01">
<Description>Europa Test 2001 (Apr00-Mar01) - +Global - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle>Pop</WeightTitle>
<TotalWeight>1000</TotalWeight>
<TotalSample>500</TotalSample>
<LanguageCount>5</LanguageCount>
<Language id="ENG">
<Description>English (United States)</Description>
</Language>
<Language id="FRA">
<Description>French (France)</Description>
</Language>
<Language id="ITA">
<Description>Italian (Italy)</Description>
</Language>
<Options>
<SupportsAlphaSearch />
<Barnardisation />
<ReconcileUniverse />
<RFModelType />
</Options>
<AccessCodes>
<CODE>@@@@</CODE>
</AccessCodes>
</Survey>
<Survey id="Eu02r1">
<Description>Europa Test 2002 (Oct00-Sept01) - +Global - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle>Pop</WeightTitle>
<TotalWeight>1000</TotalWeight>
<TotalSample>500</TotalSample>
<LanguageCount>5</LanguageCount>
<Language id="ENG">
<Description>English (United States)</Description>
</Language>
<Language id="FRA">
<Description>French (France)</Description>
</Language>
<Language id="DEU">
<Description>German (Germany)</Description>
</Language>
<Language id="ITA">
<Description>Italian (Italy)</Description>
</Language>
<Language id="ESP">
<Description>Spanish (Traditional Sort)</Description>
</Language>
<Options>
<SupportsAlphaSearch />
<Barnardisation />
<ReconcileUniverse />
<RFModelType />
</Options>
<AccessCodes>
<CODE>ACR1</CODE>
<CODE>ACR2</CODE>
<CODE>ACR3</CODE>
<CODE>APPL</CODE>
</AccessCodes>
</Survey>
<Surveys>
However, your code is producing the following result:
<?xml version="1.0" standalone="yes"?>
<Surveys xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Survey id="Eu02r1">
<Description>Europa Test 2002 (Oct00-Sept01) - +Global - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle>Pop</WeightTitle>
<TotalWeight>1000</TotalWeight>
<TotalSample>500</TotalSample>
<RegionCodes>5</RegionCodes>
<WeightLabel>Eu02r1</WeightLabel>
<WeightUnits>2</WeightUnits>
<Language ID="">
<Description />
</Language>
<Options>
<SupportsAlphaSearch />
<Barnardisation />
<ReconcileUniverse />
<RFModelType />
</Options>
<AccessCodes>
<CODE>ACR1</CODE>
<CODE>ACR2</CODE>
<CODE>ACR3</CODE>
<CODE>APPL</CODE>
</AccessCodes>
</Survey>
<Survey id="Eu01">
<Description>Europa Test 2001 (Apr00-Mar01) - +Global - pop</Description>
<Copyright>Test International 2001</Copyright>
<WeightTitle>Pop</WeightTitle>
<TotalWeight>1000</TotalWeight>
<TotalSample>500</TotalSample>
<RegionCodes>5</RegionCodes>
<WeightLabel>Eu01</WeightLabel>
<WeightUnits>1</WeightUnits>
<Language ID="">
<Description />
</Language>
<Options>
<SupportsAlphaSearch />
<Barnardisation />
<ReconcileUniverse />
<RFModelType />
</Options>
<AccessCodes>
<CODE>@@@@</CODE>
</AccessCodes>
</Survey>
</Surveys>
Basically, It is missing the <Language><Description> nodes.
Do you have any ideas on how I could sove that??
Cheers,
Claudio
|
|

February 21st, 2005, 04:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I told,DataSet can't parse nested tags,in your case you should try XML classes for parsing your XML file.
_____________
Mehdi.
software student.
|
|

February 21st, 2005, 05:30 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Ok Mehdi,
Thanks a lot. I will keep trying.
OH, I was reading about CreateChildView for nested tags in a dataset and also about a dataset relations.
DS.Relations
Container.DataItem.CreateChildView(..the relations goes here..)
I will let you know if I can figure out another way of doing that.
By the way, do you know any other good forum for c# and ASP.NET??
Cheers,
Claudio
|
|
 |