Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to write to an existing XML file for C#

Tags:

c#

xml

Thank you very much in advance

This is the original XML File

<my:Incident>
       <my:Category>This is for Category</my:Category>
              <my:Status>`Status is Close`</my:Status>
       <my:Description>`This is the description part</my:Description>
</my:Incident>

and I would like to add other fields under my:Incident

This is an example of it:

<my:Incident>
       <my:Category>This is for Category</my:Category>
              <my:Status>`Status is Close`</my:Status>
       <my:SummaryDescription>This is the summary</my:SummaryDescription>
       <my:Description>`This is the description part</my:Description>
</my:Incident>

I tried to implemented but I got this error message:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

public void writerXMLTest(string fileName)
        {

            if (!File.Exists(fileName))
            {
                XmlTextWriter writer = new XmlTextWriter(fileName, null);
                writer.WriteStartElement("my:Incident");
                writer.WriteEndElement();
                writer.Close();
            }

            XDocument doc = XDocument.Load(fileName);
            XElement demoNode = new XElement("my:Incident");
            demoNode.Add(new XElement("my:SummaryDescription", "Test Test"));


            Console.WriteLine("I write it!!!!!"); 
        }

I would appreciate if anyone can guide me where I did wrong in my code.


I modified the code a little. But now I'm not able to write it to the existing XML File

This is my code:

public void writerXMLTest(string fileName)
        {

            if (!File.Exists(fileName))
            {
                XmlTextWriter writer = new XmlTextWriter(fileName, null);
                writer.WriteStartElement("Incident", "my");
                writer.WriteEndElement();
                writer.Close();
            }

            XDocument doc = XDocument.Load(fileName);
            XElement demoNode = new XElement("SummaryDescription", "Test Test");


            Console.WriteLine("I write it!!!!!"); 
        }
like image 465
yyc2001 Avatar asked Dec 08 '11 20:12

yyc2001


People also ask

How do I edit an existing XML file?

From the Project menu, select Add New Item. Select XML File from the Templates pane. Enter the filename in the Name field and press Add. The XML file is added to the project and opens in the XML editor.

Can you update an XML file?

To update data in an XML column, use the SQL UPDATE statement. Include a WHERE clause when you want to update specific rows. The entire column value will be replaced. The input to the XML column must be a well-formed XML document.


1 Answers

This is wrong:

 writer.WriteStartElement("my:Incident");

This is right:

 writer.WriteStartElement("Incident", "blablablaSpace:my");

Edit:

 writer.WriteStartElement("Incident", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-09-22T20:42:56:my");
like image 147
FailedDev Avatar answered Sep 17 '22 17:09

FailedDev