none
create separate XML file based on html tags in a string in C# RRS feed

  • Question

  • I have a long string like  <abcd><xyz>testingmessage like this</xyz></abcd><abcd> <xyz>testingmessage like this</xyz></abcd><abcd> <xyz>testingmessage like this</xyz></abcd><abcd> <xyz>testingmessage like this</xyz></abcd>

    I want to create a separate xml file based on <abcd><xyz> and </xyz></abcd>these xml tags eg : <abcd><xyz>testingmessage like this</xyz></abcd>consider as one xml file . I know it's a substring search in a string in C# but is there any fastest way to do this or any other way to achieve this . Any clue regarding this will be helpful . Please help . I have to do this in C# or can I do this using javascript .

    EDIT I want to create separate xml file based on <abcd><xyz> and </xyz></abcd> these xml tags in a string .


    Saturday, July 21, 2018 5:41 AM

All replies

  • If you prepend and append the missing elements like “<xml>” + your-long-string + “</xml>”, then it will look like an XML file, which can be saved, parsed, etc.

    Show the expected contents of the separate XML file.


    • Proposed as answer by Stanly Fan Wednesday, August 1, 2018 6:11 AM
    Saturday, July 21, 2018 12:34 PM
  • Hi Saha,

    >>I want to create separate xml file based on <abcd><xyz> and </xyz></abcd> these xml tags in a string .

    Try this:

            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(dec);
                //create a root
                XmlElement root = doc.CreateElement("MyXML");
                doc.AppendChild(root);
    
                //create a node
                XmlNode node = doc.CreateElement("abcd");
                //create an element
                XmlElement element1 = doc.CreateElement("xyz");
                element1.SetAttribute("Name", "Test1");
                element1.SetAttribute("ID", "01");
                element1.InnerText = "testingmessage like this";
                node.AppendChild(element1);
    
                XmlNode node2 = doc.CreateElement("abcd");
                XmlElement element2 = doc.CreateElement("xyz");
                element2.SetAttribute("Name", "Test2");
                element2.SetAttribute("ID", "02");
                element2.InnerText = "testingmessage like this";
                node2.AppendChild(element2);
    
                root.AppendChild(node);
                root.AppendChild(node2);
                doc.Save(@"D:\test.xml");
    
                Console.WriteLine("Done.");
                Console.ReadKey();
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 23, 2018 3:07 AM