简体   繁体   中英

Export an XML content from a TextBox into an XML file

I am trying to create an application that imports an XML file into a TextBox, with the goal to edit the content. After editing, the user should be able to save the content of the file, but at the same time to validate it. For example,

<Person id="22">
    <Name gg="u">John</Name>
    <Surname>Jones</Surname>
    <PhoneNo>333333333111</PhoneNo>
    <Country>Germany</Country>
</Person>

If the user edits the start tag "Name", but forgets to edits the end tag, it should throw an exception. I have tried

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(MyTextBox.Text);
xmlDoc.Save(fileName);

and

XmlElement DocRoot = xmlDoc.CreateElement("root");
DocRoot.InnerText = MyTextBox.Text;
xmlDoc.AppendChild(DocRoot);
xmlDoc.Save(fileName);

None worked. I am grateful for any help, thank you!

I have though of this solution and it seems to work :) As per the xsd question, I have a generic XML.

try
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(MyTextBox.Text);
        XmlWriterSettings settings = new XmlWriterSettings
        {
        Indent = true
    };
    XmlWriter writer = XmlWriter.Create(fileName, settings);
    xmlDoc.Save(writer);
    MessageBox.Show("File overwritten to: " + fileName);
    }
    catch (Exception ex)
    {
    MessageBox.Show("The textbox content is wrong. ");
    }

Seems that you are trying to check if the XML text is well formed and not really if it is valid against a certain definition.

To check if the XML text is well formed, you can try to parse it and verify if it contains any errors. Here is a function that attempts to do that:

class Program
{
    static void Main(string[] args)
    {
        var result = ValidateXml("<Person id=\"22\"><Name>John<Name></Person>");
        if (!result.IsValid)
        {
            Console.WriteLine($"Line number: {result.Exception.LineNumber}");
            Console.WriteLine($"Line position: {result.Exception.LinePosition}");
            Console.WriteLine($"Message: {result.Exception.Message}");
        }

        // OUTPUT:
        // Line number: 1
        // Line position: 35
        // Message: The 'Name' start tag on line 1 position 28 does not match the end tag of 'Person'.Line 1, position 35.
    }

    static ValidationResult ValidateXml(string xml)
    {
        using (var xr = XmlReader.Create( new StringReader(xml)))
        {
            try
            {
                while (xr.Read())
                {
                }

                return ValidationResult.ValidResult;
            }
            catch (XmlException exception)
            {
                return new ValidationResult(exception);
            }
        }
    }

    public class ValidationResult
    {
        public static ValidationResult ValidResult = new ValidationResult();

        private ValidationResult()
        {
            IsValid = true;
        }

        public ValidationResult(XmlException exception)
        {
            IsValid = false;
            Exception = exception;
        }

        public bool IsValid { get; }

        public XmlException Exception { get;}
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM