cutie2000
asked on
Save to XML
I have this piece of code.
public static void SaveXML()
{
XmlDocument xmlDoc;
XmlNode decNode;
XmlElement rootElement;
XmlElement level2Element;
xmlDoc = new XmlDocument();
// Create and add the XML declaration
decNode = xmlDoc.CreateNode(XmlNodeT ype.XmlDec laration, null, null);
xmlDoc.AppendChild(decNode );
// Create and add the root element Document
rootElement = xmlDoc.CreateElement("Mail ");
rootElement.SetAttribute(" xmlns:xsi" , "http://www.w3.org/2001/XMLSchema-instance");
rootElement.SetAttribute(" xsi:noName spaceSchem aLocation" , "mailValidate.xsd");
xmlDoc.AppendChild(rootEle ment);
// Create a new To element and add it to the Root element
level2Element = xmlDoc.CreateElement("To") ;
level2Element.InnerText = "to@email.com";
rootElement.AppendChild(le vel2Elemen t);
// Create a new Cc element and add it to the Root element
level2Element = xmlDoc.CreateElement("Cc") ;
level2Element.InnerText = "cc@email.com";
rootElement.AppendChild(le vel2Elemen t);
// Create a new Bcc element and add it to the Root element
level2Element = xmlDoc.CreateElement("Bcc" );
level2Element.InnerText = "bcc@email.com";
rootElement.AppendChild(le vel2Elemen t);
// Create a new From element and add it to the Root element
level2Element = xmlDoc.CreateElement("From ");
level2Element.InnerText = "from@email.com";
rootElement.AppendChild(le vel2Elemen t);
// Create a new Subject element and add it to the Root element
level2Element = xmlDoc.CreateElement("Subj ect");
level2Element.InnerText = "Subject";
rootElement.AppendChild(le vel2Elemen t);
//Save the XML document
xmlDoc.Save(@"c:\namevalue .xml");
}
However, the code is giving me below XML file
<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation= "mailValid ate.xsd">
....
I need to have it as
However, the code is giving me below XML file
<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocat ion="mailV alidate.xs d">
....
public static void SaveXML()
{
XmlDocument xmlDoc;
XmlNode decNode;
XmlElement rootElement;
XmlElement level2Element;
xmlDoc = new XmlDocument();
// Create and add the XML declaration
decNode = xmlDoc.CreateNode(XmlNodeT
xmlDoc.AppendChild(decNode
// Create and add the root element Document
rootElement = xmlDoc.CreateElement("Mail
rootElement.SetAttribute("
rootElement.SetAttribute("
xmlDoc.AppendChild(rootEle
// Create a new To element and add it to the Root element
level2Element = xmlDoc.CreateElement("To")
level2Element.InnerText = "to@email.com";
rootElement.AppendChild(le
// Create a new Cc element and add it to the Root element
level2Element = xmlDoc.CreateElement("Cc")
level2Element.InnerText = "cc@email.com";
rootElement.AppendChild(le
// Create a new Bcc element and add it to the Root element
level2Element = xmlDoc.CreateElement("Bcc"
level2Element.InnerText = "bcc@email.com";
rootElement.AppendChild(le
// Create a new From element and add it to the Root element
level2Element = xmlDoc.CreateElement("From
level2Element.InnerText = "from@email.com";
rootElement.AppendChild(le
// Create a new Subject element and add it to the Root element
level2Element = xmlDoc.CreateElement("Subj
level2Element.InnerText = "Subject";
rootElement.AppendChild(le
//Save the XML document
xmlDoc.Save(@"c:\namevalue
}
However, the code is giving me below XML file
<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation=
....
I need to have it as
However, the code is giving me below XML file
<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocat
....
ASKER
I need more help.
This is giving me exception.
XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.None;
settings.OmitXmlDeclaratio n = false;
settings.CheckCharacters = false;
settings.CloseOutput = true;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Encoding = new UTF8Encoding();
settings.Indent = true;
XmlWriter writer = XmlTextWriter.Create(fileN ame, settings);
writer.WriteStartDocument( );
// <Root>
writer.WriteStartElement(" Mail");
writer.WriteAttributeStrin g("xmlns:x si", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeStrin g("xmlns", "http://www.w3.org/2001/XMLSchema-instance");
// </Root>
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
This is giving me exception.
XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.None;
settings.OmitXmlDeclaratio
settings.CheckCharacters = false;
settings.CloseOutput = true;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Encoding = new UTF8Encoding();
settings.Indent = true;
XmlWriter writer = XmlTextWriter.Create(fileN
writer.WriteStartDocument(
// <Root>
writer.WriteStartElement("
writer.WriteAttributeStrin
writer.WriteAttributeStrin
// </Root>
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
I didn't write the complete XML code for you, so what is the exception?
Bob
Bob
ASKER
Thsi is the exception
System.ArgumentException was unhandled
Message="Invalid name character in 'xmlns:xsi'. The ':' character, hexadecimal value 0x3A, cannot be included in a name."
Source="System.Xml"
StackTrace:
at System.Xml.XmlWellFormedWr iter.Check NCName(Str ing ncname)
at System.Xml.XmlWellFormedWr iter.Write StartAttri bute(Strin g prefix, String localName, String namespaceName)
at System.Xml.XmlWriter.Write AttributeS tring(Stri ng localName, String value)
at TestConsoleApp.Program.Sav eXML(Strin g fileName) in C:\Documents and Settings\hongjun\My Documents\Visual Studio 2005\Projects\CSharpPracti ce\Test\Te stConsoleA pp\Program .cs:line 75
at TestConsoleApp.Program.Mai n(String[] args) in C:\Documents and Settings\hongjun\My Documents\Visual Studio 2005\Projects\CSharpPracti ce\Test\Te stConsoleA pp\Program .cs:line 17
at System.AppDomain.nExecuteA ssembly(As sembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
Anyway, this is the result I want.
<?xml version="1.0"?>
<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocat ion="mailV alidate.xs d">
<To>to@email.com</To>
<Cc>cc@email.com</Cc>
<Bcc>bcc@email.com</Bcc>
<From>from@email.com</From >
<Subject>Subject</Subject>
<Body>Body</Body>
</Mail>
System.ArgumentException was unhandled
Message="Invalid name character in 'xmlns:xsi'. The ':' character, hexadecimal value 0x3A, cannot be included in a name."
Source="System.Xml"
StackTrace:
at System.Xml.XmlWellFormedWr
at System.Xml.XmlWellFormedWr
at System.Xml.XmlWriter.Write
at TestConsoleApp.Program.Sav
at TestConsoleApp.Program.Mai
at System.AppDomain.nExecuteA
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
Anyway, this is the result I want.
<?xml version="1.0"?>
<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocat
<To>to@email.com</To>
<Cc>cc@email.com</Cc>
<Bcc>bcc@email.com</Bcc>
<From>from@email.com</From
<Subject>Subject</Subject>
<Body>Body</Body>
</Mail>
ASKER
System.ArgumentException was unhandled
Message="Invalid name character in 'xmlns:xsi'.
Message="Invalid name character in 'xmlns:xsi'.
The XmlTextWriter class has a lot of power, but it is kind of hidden. The WriteStartElement has 3 overloaded methods. You would need this form:
WriteStartElement(prefix, localName, ns)
Example:
WriteStartElement("xsi", "Mail", "http://www.w3.org/2001/XMLSchema-instance" );
Bob
WriteStartElement(prefix, localName, ns)
Example:
WriteStartElement("xsi", "Mail", "http://www.w3.org/2001/XMLSchema-instance" );
Bob
ASKER
This is my code now.
I am unable to get the 2nd attribute written on the Root.
This is the one xsi:noNamespaceSchemaLocat ion="mailV alidate.xs d">
XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.None;
settings.OmitXmlDeclaratio n = false;
settings.CheckCharacters = false;
settings.CloseOutput = true;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Encoding = new UTF8Encoding();
settings.Indent = true;
XmlWriter writer = XmlTextWriter.Create(fileN ame, settings);
writer.WriteStartDocument( );
// <Mail>
//<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocat ion="mailV alidate.xs d">
writer.WriteStartElement(" xsi", "Mail", "http://www.w3.org/2001/XMLSchema-instance");
//writer.WriteAttributeStr ing("xsi", "Mail", "noNamespaceSchemaLocation ", "mailValidate.xsd");
writer.WriteElementString( "To", "to@email.com");
writer.WriteElementString( "Cc", "cc@email.com");
writer.WriteElementString( "Bcc", "bcc@email.com");
writer.WriteElementString( "From", "from@email.com");
writer.WriteElementString( "Subject", "Subject");
writer.WriteElementString( "Body", "This is body");
// </To>
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
I am unable to get the 2nd attribute written on the Root.
This is the one xsi:noNamespaceSchemaLocat
XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.None;
settings.OmitXmlDeclaratio
settings.CheckCharacters = false;
settings.CloseOutput = true;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Encoding = new UTF8Encoding();
settings.Indent = true;
XmlWriter writer = XmlTextWriter.Create(fileN
writer.WriteStartDocument(
// <Mail>
//<Mail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocat
writer.WriteStartElement("
//writer.WriteAttributeStr
writer.WriteElementString(
writer.WriteElementString(
writer.WriteElementString(
writer.WriteElementString(
writer.WriteElementString(
writer.WriteElementString(
// </To>
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
Try this:
writer.WriteAttributeStrin g("noNames paceSchema Location", "http://www.w3.org/2001/XMLSchema-instance", "mailValidate.xsd");
Bob
writer.WriteAttributeStrin
Bob
ASKER
bob, thanks a lot
however, i realized something
<xsi:Mail xsi:noNamespaceSchemaLocat ion="mailV alidate.xs d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
seems to be different from
<Mail xsi:noNamespaceSchemaLocat ion="mailV alidate.xs d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Notice the extra xsi: generated by the .NET code. If I remove one of the TAG from my generated XML file, VS 2005 does not flag a validation error if I have xsi: in front. If I don't have, VS 2005 correctly flag an error. So I am wondering is it possible to generate without the xsi in front?
however, i realized something
<xsi:Mail xsi:noNamespaceSchemaLocat
seems to be different from
<Mail xsi:noNamespaceSchemaLocat
Notice the extra xsi: generated by the .NET code. If I remove one of the TAG from my generated XML file, VS 2005 does not flag a validation error if I have xsi: in front. If I don't have, VS 2005 correctly flag an error. So I am wondering is it possible to generate without the xsi in front?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.None;
settings.OmitXmlDeclaratio
settings.OutputMethod = XmlOutputMethod.Xml;
settings.CheckCharacters = false;
settings.CloseOutput = true;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Encoding = new UTF8Encoding();
settings.Indent = true;
XmlTextWriter writer = XmlTextWriter.Create(fileN
writer.WriteStartDocument(
// <Root>
writer.WriteStartElement("
// </Root>
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
Bob