How can I insert the following stylesheet information into my existing xml file which is created using C#?
<?xml-stylesheet type="text/xsl" href="_fileName.xsl"?>
Or.... Can I add this line at the time of creation of the new XML file?
Edit:
I tried to achieve the above using XmlSerialier (hit and trial), something like this:
// assumes 'XML' file exists.
XmlDocument doc = new XmlDocument();
XElement dataElements = XElement.Load("_fileName.xml");
XmlSerializer xs = new XmlSerializer(typeof(Parents));
var ms = new MemoryStream();
xs.Serialize(ms, parents);
ms.Seek(0, SeekOrigin.Begin); // rewind stream to beginning
doc.Load(ms);
XmlProcessingInstruction pi;
string data = "type=\"text/xsl\" href=\"_fileName.xsl\"";
pi = doc.CreateProcessingInstruction("xml-stylesheet", data);
doc.InsertBefore(pi, doc.DocumentElement); // insert before root
doc.DocumentElement.Attributes.RemoveAll(); // remove namespaces
But the output xml is getting corrupted:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="_fileName.xsl"?>
<parents />
Whereas the desired output is something like:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="_fileName.xsl"?>
<parents>
<parent>
<Child1>
<child2>
</parent>
</parents>
Did this help to understand what's my problem???
You didn't answer the question.. "what lib do you use".
Although I advise:
XDocument
if you would use it you could do something like:
XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
document.Add(new XProcessingInstruction(
"xml-stylesheet", "type=\"text/xsl\" href=\"_fileName.xsl\""));
//and then your actual document...
document.Add(
new XElement("parent",
new XElement("child1"),
new XElement("child2")
)
);
EDIT:
Ok So you could do it like:
XDocument document = XDocument.Load("file");
document.AddFirst(new XProcessingInstruction(
"xml-stylesheet", "type=\"text/xsl\" href=\"LogStyle.xsl\""));
Is this what you're looking for?
Related
I have done this code so far works well, but the only issue is that the XML file gets deleted every time I use the method addUser, I want the code to continue writing under it
the code I wrote in c#:
public static void AddUser(PersonData pd)
{
XmlTextWriter xWriter = new XmlTextWriter("D:\\PersonData.Xml", Encoding.UTF8);
xWriter.Formatting = Formatting.Indented;
xWriter.WriteStartElement("User");
xWriter.WriteAttributeString("idNumber", pd.IdNumber1);
xWriter.WriteStartElement("Firstname");
xWriter.WriteString(pd.FirstName1);
xWriter.WriteEndElement();//<FirstName>
xWriter.WriteStartElement("LastName");
xWriter.WriteString(pd.LastName1);
xWriter.WriteEndElement();//<LastName>
xWriter.WriteStartElement("DateOfBirth");
xWriter.WriteString(pd.DateOfBirth1.ToString());
xWriter.WriteEndElement();//<DateOfBirth>
xWriter.WriteStartElement("Address");
xWriter.WriteString(pd.Address1);
xWriter.WriteEndElement();//<Address>
xWriter.WriteEndElement();//<user>
xWriter.Close();
}
the XML output:
<User idNumber="316447077">
<Firstname>majd</Firstname>
<LastName>sadi</LastName>
<DateOfBirth>29/03/1998 14:54:50</DateOfBirth>
<Address>hohos</Address>
</User>
the XML output that I want to do:
<User idNumber="316447077">
<Firstname>majd</Firstname>
<LastName>sadi</LastName>
<DateOfBirth>29/03/1998 14:54:50</DateOfBirth>
<Address>hohos</Address>
</User>
<User idNumber="316447077">
<Firstname>majd</Firstname>
<LastName>sadi</LastName>
<DateOfBirth>29/03/1998 14:54:50</DateOfBirth>
<Address>hohos</Address>
</User>
Assuming you have a root node to your XML (not valid XML without it), you could do this with XElement:
XElement xe;
if (File.Exists("D:\\PersonData.Xml"))
xe = XElement.Load("D:\\PersonData.Xml");
else
xe = new XElement("Users");
XElement newUser = new XElement("User");
newUser.Add(new XAttribute("idNumber", pd.IdNumber1));
newUser.Add(new XElement("Firstname", pd.FirstName1));
newUser.Add(new XElement("LastName", pd.LastName1));
newUser.Add(new XElement("DateOfBirth", pd.DateOfBirth1.ToString()));
newUser.Add(new XElement("Address", pd.Address1));
xe.Add(newUser);
xe.Save("D:\\PersonData.Xml");
try to flush like xWriter.Flush() after you write your end element?
I have used XDocument to create simple xml document.I have created document with XDocument and XDeclaration.
XDocument encodedDoc8 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Root", "Content"));
If i save this document to file means the encoding type is not changed.
using (TextWriter sw = new StreamWriter(#"C:\sample.txt", false)){
encodedDoc8.Save(sw);
}
Output:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>Content</Root>
But,if I use XDocument's WriteTo method to print the xml means encoding type is changed.
using (XmlWriter writ = XmlWriter.Create(Console.Out))
{
encodedDoc8.WriteTo(writ);
}
Output:
<?xml version="1.0" encoding="IBM437" standalone="yes"?><Root>Content</Root>
Why this happened?.Please update your answers.Thanks in advance.
If you look at the reference source for XmlWriter.Create, the chain of calls would eventually lead to this constructor:
public XmlTextWriter(TextWriter w) : this() {
textWriter = w;
encoding = w.Encoding;
xmlEncoder = new XmlTextEncoder(w);
xmlEncoder.QuoteChar = this.quoteChar;
}
The assignment encoding = w.Encoding provides an explanation to what is happening in your case: the Encoding setting of the Console.Out text writer is copied to the encoding setting of the newly created XmlTextWriter, replacing the encoding that you supplied in the XDocument.
I am writing a XML using XmlDocument, I need a element or attribute like shown below
The element or attribute required is <?Validversion="1" ?>
how to create using xmldocument or xmlwriter.
// to create <?Validversion="1" ?>
XmlDocument aDoc = new XmlDocument();
aDoc.CreateXmlDeclaration("1.0", "utf-16", null);
XmlCDataSection aDataSec =aDoc.CreateCDataSection("?Version = 2");
aDoc.AppendChild(aDataSec);
aDoc.Save("c:\\vector.xml");
You are looking for XmlDocument.CreateProcessingInstruction and not CDATA section:
var document = new XmlDocument();
document.AppendChild(document.CreateXmlDeclaration("1.0", "utf-16", null));
var piNode = document.CreateProcessingInstruction("Version", "=\"2\"");
document.AppendChild(pi);
Side note: don't forget to AppendChild newly created node.
When I'm trying to edit XML Element and save it, it generates copy (with edited element) and appends it to end of file.
var localStore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("DataFolder\\PlayerData.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, localStore);
var doc = XDocument.Load(stream);
doc.Root.Element("characters").Element("character").SetElementValue("expierence", 10);
doc.Save(stream, SaveOptions.None);
stream.Close();
Example output file:
<?xml version="1.0" encoding="utf-8"?>
<root>
<characters>
<character>
<expierence>0</expierence>
</character>
</characters>
</root><?xml version="1.0" encoding="utf-8"?>
<root>
<characters>
<character>
<expierence>10</expierence>
</character>
</characters>
</root>
That's exactly what you told it to do by passing FileMode.OpenOrCreate.
If you want to truncate any existing file, pass Create.
For more information, see the documentation.
I need to add the following code to the beginning of an XML file, while creating it:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="colors.xslt"?>
I'm sure there is a method for this, but I haven't found it yet. I'm using C#.
Thank you
XmlDocument.CreateProcessingInstruction Method
public static void Main()
{
var doc = new XmlDocument();
doc.AppendChild(doc.CreateProcessingInstruction(
"xml-stylesheet",
"type='text/xsl' href='colors.xslt'"));
}
For LINQ XDocument you can use this:
XDocument xDoc = new XDocument();
xDoc.Add(new XProcessingInstruction("xml-stylesheet",
"type=\"text/xsl\" href=\"xml-style.xslt\""));