need help to read xml file. i have one xml file which i want to read and store in data base below is my xml code
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<SctiesSttlmTxStsAdvc>
<Id>
<Id>ACK0000000000004</Id>
<CreDtTm>
<Dt>2011-08-26</Dt>
</CreDtTm>
</Id>
<TxId>
<AcctOwnrTxId>TCP-CMF001-000000004</AcctOwnrTxId>
</TxId>
<PrcgSts>
<AckdAccptd>
<NoSpcfdRsn>NORE</NoSpcfdRsn>
</AckdAccptd>
</PrcgSts>
<MsgOrgtr>
<PrtryId>
<Id>068001</Id>
<Issr>BMSC</Issr>
</PrtryId>
</MsgOrgtr>
<MsgRcpt>
<PrtryId>
<Id>056001</Id>
<Issr>BMSC</Issr>
</PrtryId>
</MsgRcpt>
</SctiesSttlmTxStsAdvc>
<SctiesSttlmTxStsAdvc>
</SctiesSttlmTxStsAdvc>
</Document>
above xml contains tags which occur multiple time and i need to find out the value of all child node which has value
I think it should get you all the child nodes from your root node
XmlDocument doc = new XmlDocument();
doc.LoadXml("nameofyourfile.xml");
XmlNode root = doc.FirstChild;
//Display the contents of the child nodes.
if (root.HasChildNodes)
{
for (int i=0; i<root.ChildNodes.Count; i++)
{
if(root.ChildNodes[i].InnerText!="")
{
Console.WriteLine(root.ChildNodes[i].InnerText);
}
}
}
}
}
you can load xm directly like this
doc.LoadXml(<Document>
<SctiesSttlmTxStsAdvc>
<Id>
<Id>ACK0000000000004</Id>
<CreDtTm>
<Dt>2011-08-26</Dt>
</CreDtTm>
</Id>
<TxId>
<AcctOwnrTxId>TCP-CMF001-000000004</AcctOwnrTxId>
</TxId>
<PrcgSts>
<AckdAccptd>
<NoSpcfdRsn>NORE</NoSpcfdRsn>
</AckdAccptd>
</PrcgSts>
<MsgOrgtr>
<PrtryId>
<Id>068001</Id>
<Issr>BMSC</Issr>
</PrtryId>
</MsgOrgtr>
<MsgRcpt>
<PrtryId>
<Id>056001</Id>
<Issr>BMSC</Issr>
</PrtryId>
</MsgRcpt>
</SctiesSttlmTxStsAdvc>
<SctiesSttlmTxStsAdvc>
</SctiesSttlmTxStsAdvc>
</Document>);
Related
<?xml version="1.0" encoding="utf-8"?>
<response list="true">
<audio>
<aid>253663595</aid>
<artist>Example</artist>
<duration>389</duration>
<lyrics_id>57485771</lyrics_id>
<genre>18</genre>
</audio>
<audio>
<aid>253663595</aid>
<artist>Example1</artist>
<duration>400</duration>
<lyrics_id>57485772</lyrics_id>
<genre>20</genre>
</audio>
</response>
Source code
XmlDocument allAudio = new XmlDocument();
allAudio.Load(#"e:\Audio.xml");
StreamWriter write_text = File.CreateText(#"e:\Audio.txt");
XmlNodeList audioNodes = allAudio.GetElementsByTagName("audio");
foreach (XmlNode audioNode in audioNodes)
{
XmlNode artistNode = audioNode["artist"];
write_text.WriteLine(String.Format("{0}", artistNode.Value));
}
write_text.Close();
Hi, I can't parse xml. I want write some node values in file, but as result I get an empty txt file.
You want: artistNode.InnerText
Not: artistNode.value
I have an XML doc which looks roughly like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Multiple xmlns:ns2="someNs2" xmlns="someGenericNs" xmlns:ns4="someNs4" xmlns:ns3="someNs3">
<Single>
<Id>60000</Id>
<Type>Activate</Type>
<Payload>
<ns3:Activation>
<ns3:Parent>
<ns3:TypeId>113</ns3:TypeId>
<ns3:TypeName>TestApplication</ns3:TypeName>
</ns3:Parent>
<ns3:Children>
<ns3:Child>
<ns3:Key>someKey</ns3:Key>
<ns3:ChildTypeName>BadAppType1</ns3:ChildTypeName>
</ns3:Child>
<ns3:Child>
<ns3:Key>someOtherKey</ns3:Key>
<ns3:ChildTypeName>GoodAppType1</ns3:ChildTypeName>
</ns3:Child>
</ns3:Children>
</ns3:Activation>
</Payload>
</Single>
</Multiple>
If the file contains multiple "Child" nodes i would like to split it into more files, 1 file for each existing child node. Something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Multiple xmlns:ns2="someNs2" xmlns="someGenericNs" xmlns:ns4="someNs4" xmlns:ns3="someNs3">
<Single>
<Id>60000</Id>
<Type>Activate</Type>
<Payload>
<ns3:Activation>
<ns3:Parent>
<ns3:TypeId>113</ns3:TypeId>
<ns3:TypeName>TestApplication</ns3:TypeName>
</ns3:Parent>
<ns3:Children>
<ns3:Child>
<ns3:Key>someOtherKey</ns3:Key>
<ns3:ChildTypeName>GoodAppType2</ns3:ChildTypeName>
</ns3:Child>
</ns3:Children>
</ns3:Activation>
</Payload>
</Single>
</Multiple>
And then a new XmlDoc containing the other "Child" node. Can this be achieved through LINQ?
My code so far is:
private bool HasMoreThanOneChild(string xml) {
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
if (doc.GetElementsByTagName("ns3:Child").Count > 1)
{
return true;
}
return false;
}
public List<string> DoSomething(XmlDocument doc){
if(HasMoreThanOneChild(doc.InnerXml))
return Split(doc);
}
I returned a List there because I'm more interested in the "InnerXML" once the doc is split. But I am at a loss of how to implement this split, if it is possible.
XNamespace ns="someNs3";
//get all Child elements
var childElements=doc.Descendants().Elements(ns+"Child").ToList();
//remove those child elements
doc.Descendants().Elements(ns+"Child").ToList().ForEach(x=>x.Remove());
int i=1;
foreach(var child in childElements)
{
//add that child to children element
doc.Descendants().Elements(ns+"Children").First().Add(child);
//save it to new file!
doc.Save("file"+i+".xml");
doc.Descendants().Elements(ns+"Child").ToList().ForEach(x=>x.Remove());
i++;
}
I am trying to add an XML node to multiple parent nodes(which have same name). But it is only adding to the Last node of the XML and not in all.
input XML
<Record>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
</Emp>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
</Emp>
</Record>
I want to add the Location element to every Emp node. My code is as below:
XmlNodeList xNodeList = doc.SelectNodes("/Record/Emp");
XmlElement xNewChild = doc.CreateElement("Location");
xNewChild.InnerText = "USA";
foreach (XmlNode item in xNodeList)
{
item.AppendChild(xNewChild);
}
doc.Save(path);
but I am getting output like this:
<Record>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
</Emp>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
<Location>USA</Location>
</Emp>
</Record>
The Location element has not been added to the first Emp node.
Note: After debugging, I am able to find that the element has been added even for the first Emp node. But, in the saved XML file I am seeing this strange behavior.
Your xNewChild is a single new element. Simply adding it to multiple nodes will only serialize to the last node. A change like this should work:
XmlNodeList xNodeList = doc.SelectNodes("/Record/Emp");
foreach (XmlNode item in xNodeList)
{
XmlElement xNewChild = doc.CreateElement("Location");
xNewChild.InnerText = "USA";
item.AppendChild(xNewChild);
}
doc.Save(path);
Here is my XML :
<?xml version="1.0" encoding="utf-8" ?>
<Selection>
<ID>1</ID>
<Nom>Name 1</Nom>
<DateReference>0</DateReference>
<PrefixeMedia>Department</PrefixeMedia>
<FormatExport>1630</FormatExport>
<TraceAuto>Oui</TraceAuto>
<SubID></SubID>
</Selection>
<Selection>
<ID>2</ID>
<Nom>Name 1</Nom>
<DateReference>0</DateReference>
<PrefixeMedia>Department</PrefixeMedia>
<FormatExport>1630</FormatExport>
<TraceAuto>1</TraceAuto>
<SubID>1</SubID>
</Selection>
My problem is I would like to modify for example the node content of <Nom>Name 1</Nom> which is located in <Selection></Selection> which have <ID>1</ID> (Search by ID)
I'm using XElement and XDocument to do simple search but I need some help to solve this problem above. (Developpment on SilverLight
Best Regards.
Another way to do this is using XmlDocument:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"\path\to\file.xml");
// Select the <nom> node under the <Selection> node which has <ID> of '1'
XmlNode name = xmlDoc.SelectSingleNode("/Selection[ID='1']/Nom");
// Modify the value of the node
name.InnerText = "New Name 1";
// Save the XML document
xmlDoc.Save(#"\path\to\file.xml");
If you don't know how to get at the correct <Nom> node to update, the trick is to first select a <Selection> node that contains the correct <ID> node, then you can get that <Nom> node.
Something like:
XElement tree = <your XML>;
XElement selection = tree.Descendants("Selection")
.Where(n => n.Descendants("ID").First().Value == "1") // search for <ID>1</ID>
.FirstOrDefault();
if (selection != null)
{
XElement nom = selection.Descendants("Nom").First();
nom.Value = "Name one";
}
Note 1: By using Descendants("ID").First() I expect every Selection node to contain an ID node.
Note 2: And every Selection node contains a Nom node
Note 3: Now you still have to store the whole XML, if that's what you need.
I'm navigating XML document with XPathNodeIterator, and I want to change some nodes' values.
I can't figure out how :(
Here's the code I'm using:
XPathDocument docNav = new XPathDocument(path);
XPathNavigator nav = docNav.CreateNavigator();
nav.MoveToRoot();
XPathNodeIterator itemsIterator = nav.Select("/foo/bar/item");
while (mediumsIterator.MoveNext())
{
XPathNodeIterator subitemsIterator = itemsIterator.Current.Select("SubitemsList/name");
while (subitemsIterator.MoveNext())
{
XPathNodeIterator nodesIterator = itemsIterator.Current.Select("Param");
nodesIterator.MoveNext();
String the_params = nodesIterator.Current.Value;
// check if I need to modify nodesIterator.Current.Value
// ...
// ok I do - how?
}
}
And the XML file sample:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>
<item>
<Param />
<SubitemsList>
<name>name one</name>
<name>name two</name>
...
</SubitemsList>
</item>
...
</bar>
</foo>
Or maybe there's a better way to do this?
I found a way:
Replace XPathDocument with XmlDocument
When I get to the needed node
...
XmlNode node = ((IHasXmlNode)nodesIterator.Current).GetNode();
node.InnerText = "new text";