Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 2 of 2

Thread: Need help with xml

  1. #1

    Need help with xml

    HI experts,
    I have one question related with xml.i have an xml file of this format.Members being the root element and three elements by name member.Each member will having different child elements and attributes as well.
    <Members>
    <member>
    </member>
    <member>
    </member>
    <member>
    </member>
    </Members>
    I need to split each member element tag to another xml file and save the xml file to hard disk.
    Here is the code snippet i have been working on
    XmlDocument doc;
    DataSet ds = newDataSet();
    ds.ReadXml(Server.MapPath(
    "Members.xml"));
    int i;

    XmlDocument objFormattedXML = null;
    for (i = 0; i <= (ds.Tables[0].Rows.Count - 1); i++)
    {
    objFormattedXML =
    newXmlDocument();
    XmlDeclaration objDeclare = objFormattedXML.CreateXmlDeclaration("1.0", null, null);
    objDeclare = (
    XmlDeclaration)objFormattedXML.InsertBefore(objDeclare, objFormattedXML.DocumentElement);
    XmlElement objRoot = (XmlElement)objFormattedXML.CreateNode(XmlNodeType.Element, "Members", "");

    objRoot = (XmlElement)objFormattedXML.InsertAfter(objRoot, objDeclare);

    XmlNodeList Objective;
    doc =
    newXmlDocument();
    doc.Load(
    @"c:\Data\Members.xml");
    Objective = doc.SelectNodes(
    "Members/Member");
    if (Objective.Count > 0)
    {
    XmlElement Member = (XmlElement)objFormattedXML.CreateNode(XmlNodeType.Element, "Member", "");
    objRoot.AppendChild(Member).InnerXml=Objective[0].InnerXml;
    }
    objFormattedXML.Save(
    "C:\\test" + i + ".xml");
    }


    I am able to split the files, i am getting three xml files but with the same data.am i missing something here.can some assist me with this

    Advance thanks

  2. #2
    not entirely sure about how to do with with an dataset, but i would do use XmlDocument and XmlNode. Sorry but i am not going to check my work to make sure it works, but this shoudl give you an idea for a very nice solution.

    XmlDocument doc = new XmlDocument();
    doc.Load(file);

    XmlElement element1 = doc.SelectSingleNode("/Members/Member[0]");
    XmlDocument SaveDoc1 = new XmlDocument();
    SaveDoc1.Load(new StringReader(element1.Outterxml);
    SaveDoc1.Save(filepath);


    you can rinse and repeat for the other nodes. What it is doing is going creating a sub set or sub document and then loading it into its own memory space and saving it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012