locked
XML RRS feed

  • Question

  • User-266166614 posted

    I need to dump lot of XmlNode and their corresponding values, without creading one by one XmlNode, Considering them as Xml and Simply Writing XmlTag and Values as a String.

    My Quesiton is <Parent><Child>Value1</Child><Next>Value2</Next></Parent> like wise so in that way if I write all these values to Xml File would that Xml be valid.....

    This will not work.

    Now question is

    XDocument srcTree = new XDocument(
                        new XComment("This is a comment"),
                        new XElement("Root",
                            new XElement("Child1", "data1"),
                            new XElement("Child2", "data2"),
                            new XElement("Child3", "data3"),
                            new XElement("Child2", "data4"),
                            new XElement("Info5", "info5"),
                            new XElement("Info6", "info6"),
                            new XElement("Info7", "info7"),
                            new XElement("Info8", "info8")
                    ));

    Is it possible to Generalize and Add as many elements (may be 100 or 1000 elements) and these element would be repeated in some logic (logic is not required) based on looping and condition and make it structured code.

    Can someone suggest, I just need better way to do it..
     

    Monday, March 2, 2015 10:41 AM

Answers

  • User-266166614 posted

    Solved by using

    XmlNode nextSiblingNode = xNode.NextSibling;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2015 7:38 AM

All replies

  • User1508394307 posted

    Do not use linq but System.Xml methods

    XmlDocument xd = new XmlDocument();
    XmlElement root = xd.CreateElement("Root");
    for (int i = 0; i < 5; i++) 
    {
        XmlElement xe = xd.CreateElement("Child" + i);
    	xe.InnerText = "data" + i;
        root.AppendChild(xe);
    }
    xd.AppendChild(root);
    
    Console.WriteLine(xd.InnerXml);

    Hope this helps.

    Monday, March 2, 2015 11:55 AM
  • User-266166614 posted

    Thanks Smirnov.

     

    <Root>
    <Parent>
    <Child>
    <Child1>
    	<A>Data</A>
    <Child1>
    	<A>Data</A>
    <Child1>
    	<A>Data</A>
    </Child>
    </Parent>
    </Root>

    I need to loop through Child1 but everytime its modifying first record as all node names are same.
    I loop through XmNodeList but that doesn't work as desired.

     

    Thursday, March 5, 2015 5:13 AM
  • User1508394307 posted

    What is your code and what is your expected output?

    Thursday, March 5, 2015 5:33 AM
  • User-266166614 posted

     XmlNodeList Child1List = xDoc.SelectNodes("/Root/Parent/Child/Child1");

                    foreach (XmlNode xNode in Child1List
                    {
                        int i = 1;

                         node = xNode.SelectSingleNode("A");         
                        node.InnerText = newvalue;

                     }

    But looks like everytime picking same node

    Thursday, March 5, 2015 6:33 AM
  • User1508394307 posted

    Ok, I see the code, but what is the expected behavior?

    The above xml is wrong 

    <Child>
           <Child1>
    	       <A>Data</A>
                            <Child1>
    	                       <A>Data</A>
                                            <Child1>
    	                                        <A>Data</A>
    </Child>

    you need to "close" each <Child1>

    Thursday, March 5, 2015 6:59 AM
  • User-266166614 posted

    This is just sample xml I created here just to explain, original xml is fine, there is no issue in xml part.

    Thursday, March 5, 2015 7:02 AM
  • User-266166614 posted

    Expected behaviour is each time it should read next node (Child1 and then child2 like wise)

    Thursday, March 5, 2015 7:03 AM
  • User1508394307 posted

    This is just sample xml I created here just to explain, original xml is fine, there is no issue in xml part.

    What could we answer if you provided wrong example and asked why your code does not work?

    Expected behaviour is each time it should read next node (Child1 and then child2 like wise)

    You explicitely set "Child1" in your xml path.

    XmlNodeList Child1List = xDoc.SelectNodes("/Root/Parent/Child/Child1");

    Thursday, March 5, 2015 7:18 AM
  • User-266166614 posted

    Solved by using

    XmlNode nextSiblingNode = xNode.NextSibling;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2015 7:38 AM