This is Guardian.re, which is a custom XML file that I've createed in C#. I have created a new program that I want to read this file and place the info in a listbox. The xmlns being the name of the user that was entered. How do I do this?
<?xml version="1.0" encoding="utf-8"?>
<!--This is the Guardian Submit file. An admin will read with their viewers.-->
<Guardian
Age="5"
Hours="5"
Why="Why?"
Qualify="What qualifies you?"
xmlns="Name" />
Here is the C# source:
if (System.IO.File.Exists("Guardian.re") == false)
{
//.re is the file extension that is used
XmlWriter writer = XmlWriter.Create(#"Guardian.re");
writer.WriteStartDocument();
writer.WriteComment("This is the Guardian Submit file. An admin will read with their viewers.");
//Element <Guardian> in the .re XML format
writer.WriteStartElement("Guardian", IGN);
//the element <Age> in the .re XML format
writer.WriteAttributeString("Age", Age);
//the element <Hours> in the .re XML format
writer.WriteAttributeString("Hours", hours);
//the element <Why> in the .re XML format
writer.WriteAttributeString("Why", WhyRank);
//the element <Qualify> in the .re XML format
writer.WriteAttributeString("Qualify", Qualify);
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
Use XPath to get your individual values back from the file as found here
http://support.microsoft.com/kb/308333
and then add the values to the listbox as specified here
http://msdn.microsoft.com/en-us/library/aa288403(v=vs.71).aspx
You can also find some code here Parse XML and populate in List Box
I posted a possible answer for this here: Reading an XML File With Linq
Guess I probably should have waited, as the other is in regards to Linq...
Related
I am trying to change the values in a Farming simulator 22 savegame xml file from C# in visual studio. There are a lot of nodes so I have reduced them to make things easier. I want to know how to replace the value in the node using C# with out having to create and rebuild the xml file from scratch.
the path to the xml file is: (C:\Users\Name\Documents\My Games\FarmingSimulator2022\savegame1\careerSavegame.xml)
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<careerSavegame revision="2" valid="true">
<settings>
<savegameName>My game save</savegameName>
<creationDate>2022-05-03</creationDate>
<mapId>MapFR</mapId>
<mapTitle>Haut-Beyleron</mapTitle>
<saveDateFormatted>2022-08-22</saveDateFormatted>
<saveDate>2022-08-22</saveDate>
<resetVehicles>false</resetVehicles>
</careerSavegame>
You can use the System.Xml.Linq namespace to access the xml file. This will load the file in the memory.
There is one class inside it, XDocument, that represents the xml document.
String filePath = "C:\Users\Name\Documents\My Games\FarmingSimulator2022\savegame1\careerSavegame.xml"
XDocument xdoc = XDocument.Load(filePath);
var element = xdoc.Elements("MyXmlElement").Single();
element.Value = "foo";
xdoc.Save("file.xml");
You can set the element variable as per the one which is needed to be replaced.
Through some research I found the solution to editing the values within the nodes. In this example I only change the value of savegameName, but it will be the same for the rest.
//Routing the xml file
XmlDocument xmlsettings = new XmlDocument();
xmlsettings.Load(#"D:\careerSavegame.xml");
//Setting values to nodes through innertext
String FarmNameSetting = "Martek Farm";
XmlNode savegameNamenode =
xmlsettings.SelectSingleNode
("careerSavegame/settings/savegameName");
savegameNamenode.InnerText = FarmNameSetting;
I am new to C# web development. I am developing a software that receives response from webservice in XML format. (includes barcodes generated by webservice).
There is an option given by webservice provider, that i have to add a line
(Example<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">)
as a second line in the xml and display in web browser by using style sheets provided by webservice provider. If i have to choose this option, how can i add that line as second line in the received xml file also how can i map the style sheets provided by the webserive in the project for this xml.
If i dont take that option, Is it possible to display the data in xml as a pdf(includes barcodes generated by webservice), if i dont choose the option .
If I understand your question correctly, you want to:
Add a stylesheet specification to an existing XML
Convert an XML to PDF
1. ADDING A STYLESHEET
There is an option given by webservice provider, that i have to add a line [...] as a second line in the xml and display in web browser by using style sheets
This is done using e.g. Linq, like in this answer.
First of all, I think the example you used, i.e.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
may be inaccurate, as it is the first line of a XSL file (a stylesheet); those kind of files are used to transform an XML into another file (a different XML or an HTML, like in your case). However, you say
using style sheets provided by webservice provider
so my guess is that you already have those stylesheets and you can you use them, rather than creating them yourself.
If so, the line you want to add is like
<?xml-stylesheet type="text/xsl" href="helloWorld.xsl"?>
Let's suppose you already have your XML stored into an XDocument variable named "Document" with its root element being "Root"
var FilePath = "Example.xml";
var Document = XDocument.Load(FilePath);
var Root = XDocument.Descendants("Root").Single();
Then you can add your stylesheet this way, getting a new XML:
var NewDocument = new XDocument(
new XProcessingInstruction("xml-stylesheet", "type='text/xsl'ref='helloWorld.xsl'"),
Root);
2. XML to PDF
There are several ways to do this.
You might parse your XML, retrieve the elements you want to show on your PDF, use a library like iTextSharp to create a specific layout and place their contents on the file.
Or, since you already have an XML and you can transform it to an HTML using an XSL, you can use wkHtmlToPdf.
Let me know if you need more details.
How can I display XML output on ASP.NET page with XML tags?
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonOutput, "root");
Console.WriteLine(doc.OuterXml);
I would like to get results on my page like this:
<root>
<id>108013515952807</id>
<posts>
<data>
<message>This...Game... http://www.youtube.com/watch?v=l8Xsex0pqXY</message>
<id>108013515952807_470604423027046</id>
<created_time>2013-05-15T20:02:31+0000</created_time>
</data>
<data>
<message>Streaming in a few minutes! http://www.youtube.com/watch?v=IYnHDT6V82k</message>
<id>108013515952807_470538076367014</id>
<created_time>2013-05-15T16:46:36+0000</created_time>
</data>
</posts>
</root>
I tried this but I get no XML tags like in example above.
Response.Write("<BR>" + doc.OuterXml);
If you just put XML onto a webpage, the browser thinks it might be HTML and "renders" it, which is why you can't see the tags. You need to encode the XML
You can use the method
Response.Write(Server.HtmlEncode(doc.OuterXml));
Try
XElement.Parse(request.OuterXml).ToString()
If you want prettified XML string,
refer to: What is the simplest way to get indented XML with line breaks from XmlDocument?
How would I go about editing this XML file:
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee id="657434365436543" name="Joe Bloggs" group="Manager" subgroup="Deputy">
<contactDetails>
<homePhone>6535436543</homePhone>
<mobilePhone>654365436543</mobilePhone>
</contactDetails>
<personelFile>
<rightToWork>
<type>Permanent</type>
<expires>Never</expires>
</rightToWork>
<nationalInsurance>6543655543</nationalInsurance>
<startDate>01/09/2009</startDate>
</personelFile>
<holidays>
<entitlements>
<holidays>22</holidays>
<bankHolidays>8</bankHolidays>
<personalDays>1</personalDays>
</entitlements>
<taken>
<holidays>1</holidays>
<bankHolidays>0</bankHolidays>
<personalDays>0</personalDays>
</taken>
<remaining>
<holidays>21</holidays>
<bankHolidays>8</bankHolidays>
<personalDays>1</personalDays>
</remaining>
<booked>
<holidays>22</holidays>
<bankHolidays>8</bankHolidays>
<personalDays>1</personalDays>
</booked>
<remainingtobook>
<holidays>0</holidays>
<bankHolidays>0</bankHolidays>
<personalDays>0</personalDays>
</remainingtobook>
</holidays>
<shifts>
<monday>
<start>0800</start>
<end>1300</end>
</monday>
<tuesday>
<start>0800</start>
<end>1300</end>
</tuesday>
<wednesday>
<start>0800</start>
<end>1300</end>
</wednesday>
<thursday>
<start></start>
<end></end>
</thursday>
<friday>
<start>0800</start>
<end>1300</end>
</friday>
<saturday>
<start>0800</start>
<end>1200</end>
</saturday>
<sunday>
<start></start>
<end></end>
</sunday>
</shifts>
</employee>
</employees>
So far I have the following to select the correct employee from the XML:
XmlTextReader employeesReader = new XmlTextReader("Employees.xml");
var employeesXdoc = XDocument.Load(employeesReader);
var employees = from employee in employeesXdoc.Descendants("employee")
where employee.Attribute("id").Value.ToString() == employeeSelect.Value.ToString()
select new
{
nodes = employee.Nodes()
};
foreach (var employee in employees)
{
// WHAT TO PUT HERE?
}
I'm guessing I've found the right place to insert the editing for the file but then I need to know how to correctly edit it and then save it to the file without losing everything else contained there (otherwise I'd just overwrite the whole file.
Thanks for any help.
A good idea is also to use the xml-serializer-class.
There you can work with usual objects and save it to the xml later :)
http://msdn.microsoft.com/en-us/library/ms733901.aspx
Here is a useful link for updating an XML file without rewriting it in its entirety on each save:
http://support.microsoft.com/kb/301233
the general idea is to use a XMLDocument instead of a XMLTextReader and load your .xml file into the XMLDocument object. then grab the root node and start navigating/querying. once you have found the nodes you wish to edit, make your changes. then when you are done, use XMLDocument.Save(path) to save your chagnes.
IO streams are one way phenoma so you can;t use an reader/writer unless you want to overwrite the existing file.
when I am running this program, I am facing this error
public static object Load(Stream stream,Type newType)
{
//create s serializer and load the object
XmlSerializer serializer=new XmlSerializer(newType);
object newobject =serializer.Deserialize(stream);
//return the new object
return newobject;
}
?xml version="1.0"?>
-<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <FirstName>ali </FirstName> <FamilyName>bradaran</FamilyName> <UserLevel>عادی</UserLevel> <Password>123</Password> </Address>
Your problem is that there is an error in the XML document you are trying to read.
Open your XML document in Internet Explorer. If it is valid, it will display. If it is not, the error will be described and shown, which should help you track down the problem.
If the XML you posted is a genuine representation of what you're reading, there is a minus character and two semicolon characters that shouldn't be in the file. I'm also not sure you would want the xmlns attributes in your Address element?
I suggest you search for some XML tutorials on the web so you can get a better understanding of how XML must be formed.