i have the following problem!
I need to generate an Xml Document in asp.net. For this, i need to enter the following information:
- Number of tags.
- Name of each tags (example: , , , etc).
- Attributes of each tag (example: Alex... etc)..
I have already this function that received a List (TAGLIST) with the name of the xml tags.
public void functionxml()
{
string RutaSave = string.Format(#"C:\xml.xml");
var Serializer = new XmlSerializer(typeof(List<string>));
TextWriter textWriter = new StreamWriter(RutaSave);
Serializer.Serialize(textWriter, TagsList);
}
But the xml generated by this function looks like this:
<?xml version="1.0" encoding="utf-8"?>
<dictionary>
<item>
<key>
<string>CAMPO1</string>
</key>
<value>
<anyType xmlns:q1="http://www.w3.org/2001/XMLSchema" d4p1:type="q1:string" xmlns:d4p1="http://www.w3.org/2001/XMLSchema-instance">test</anyType>
</value>
</item>
<item>
<key>
<string>CAMPO2</string>
</key>
<value>
<anyType xmlns:q1="http://www.w3.org/2001/XMLSchema" d4p1:type="q1:int" xmlns:d4p1="http://www.w3.org/2001/XMLSchema-instance">2</anyType>
</value>
</item>
</dictionary>
Please, any help will be welcome!
Thanks in advance.
You should use XMLWriter instead of XMLSerializer. XMLSerializer tries to produce a XML file for an object; some kind of serialization. It is more like a BinarySerializer but it produces XML format instead of byte arrays or streams. XMLWriter is a class which writes a hierarchy into a XML file.
Related
I have below xml structure:
<?xml version="1.0" encoding="UTF-8" ?>
<rss>
<channel>
<item>
<title>Some Title</title>
</wp:comment>
<wp:comment_approved><![CDATA[1]]></wp:comment_approved>
</wp:comment>
</wp:comment>
<wp:comment_approved><![CDATA[1]]></wp:comment_approved>
</wp:comment>
</item>
</channel>
</rss>
I can easily get all wp:comments by:
xmlNode.SelectNodes("*[name()='wp:comment']")
But how can I get all wp:comments where wp:comment_approved has value 1?
it's updated #Stefan Hegny answer, as you need not comment_approvedelement, but wp:comment
xmlNode.SelectNodes("//*[name()='wp:comment'][./*[local-name() = 'comment_approved' and . = '1']")
i'm not sure if default css locators working here, but in common css xPath i'll use this locator (logic is simple - you seach some element, that contains element with special parameter, so you can adjust this locator to your needs):
//someTag[./innerTag[text() = '1']]
I have an XML file eg:
<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<id>1</id>
<details></details>
<description></description>
</item>
<item>
<id>2</id>
<details>
</details>
<description></description>
</item>
</items>
Now say suppose I want to modify an XML file such that I want to add some data to details tag for item with id=2. Using XML serializer, I would have to read the whole XML file then select item with item Id 2 and modify that class object and write whole file again? So for every update I would have to read the whole xml file into memory, then edit it in memory and then re-write as a whole to the disk?
Is there any other way to achieve this? Like could I have alogic which would simply update the node to the existing XML file?
I have an XML file which contains employee details. How can i edit the existing employee names in that MXL file using C# .NET.
Here is the xml file.
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee id="1">
<Name>Employee 1</Name>
<Designation>SE </Designation>
<Qualification>MCA </Qualification>
</Employee>
<Employee id="2">
<Name>Employee 2</Name>
<Designation>SE </Designation>
<Qualification>MCA </Qualification>
</Employee>
<Employee id="3">
<Name>Employee 3</Name>
<Designation>SE </Designation>
<Qualification>MCA </Qualification>
</Employee>
</Employees>
How can i edit the employee names. I am new to xml. For example using Console Application
You can simple do that using Linq to Xml.
using System.Xml.Linq;
...
XDocument xDoc = XDocument.Load(#"Your xml file path goes here"); // or XDocument.Parse("Your xml string goes here");
xDoc.Root.Elements("Employee").First(xe => xe.Attribute("id").Value == "1").Element("Name").Value = "your value";
Here is a good reference for head start: Programming Guide (LINQ to XML)
you can use XML Serialization, in my opinion this is the most comfortable way
of working with c# and xml,
some examples are here:
http://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx
I am currently using DOM to navigate xml in my C# project. However, some XML that i've come across lately is a bit different.
whereas usually I have:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<author>
<name>Me =)</name>
</author>
<content>
<somefield1>
<Subfield>subfield data</subfield>
</somefield>
</content>
</entry>
</feed>
and can navigate using foreach entry as entry, selectsinglenode(/content/somefield1/subfield), innertext to get the data from the subfield for each entry, the new XML looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom">
<atom:entry>
<atom:author>
<name>Me =)</name>
</atom:author>
<atom:content>
<somefield1>
<Subfield>subfield data</subfield>
</somefield>
</atom:content>
</atom:entry>
</atom:feed>
selectsinglenode(/atom:content/somefield1/subfield) is definitely not going to work...any suggestions?
atom: is just the namespace, and possibly you might just ignore it. If it still not works, you may have to use:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
selectsinglenode("atom:content/somefield1/subfield", nsmgr);
Which is documented here
I have xml wherein i have xml within it again, like:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Tag>
<Value1> </Value1>
<Value2><?xml version=\"1.0\" encoding=\"UTF-8\"?>... </Value2>
</Tag>
Deserializing doesnt work on this string in c#. I construct this string in java and send it to a c# app. how can i get around this?
The XML you show isn't well-formed. Strings need to be encoded before they are placed in the XML output stream. Your XML should look like this:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Tag>
<Value1></Value1>
<Value2><?xml version="1.0" ... </Value2>
</Tag>
One approach would be to wrap the <?xml version=\"1.0\" encoding=\"UTF-8\"?>... as a CData section:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Tag>
<Value1> </Value1>
<Value2><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>... </Value2>]]>
</Tag>
The horrible answer you didn't want to hear is that XML is more than just a string. For encoding and other reasons, you can't reliably substitute string fragments into other fragments and expect well-formed documents. If you're not using a proper XML library (which you should, by the way, they're in all the major frameworks) you can still hack XML fragment strings together by making sure their encodings are actually the same, and by removing the <?xml version="1.0" encoding="UTF-8"?> from the start of any of the fragments.