Dataset.WriteXml() namespace and prefix - c#

I have a DataSet with data that I output (write) to an XMl file.
I have added a namespace and prefix to the dataset like this:
public static string XmlNamespace = "http://namespace";
public static string XmlPrefix = "ns0";
RequestDataSet.Namespace = XmlNamespace;
RequestDataSet.Prefix = XmlPrefix;
The XML output is as follows:
<?xml version="1.0" standalone="yes"?>
<ns0:list xmlns:ns0="http://namespace">
<ns0:item xmlns="http://namespace">
<data1>1234</data1>
<data2>91011</data2>
</item>
</ns0:list>
But it should be like this: (no namespace on the item records. Just the prefix)
<?xml version="1.0" standalone="yes"?>
<ns0:list xmlns:ns0="http://namespace">
<ns0:item>
<data1>1234</data1>
<data2>91011</data2>
</item>
</ns0:list>
I have tried setting the tables prefix/namespace to null like this:
RequestDataSet.Tables["item"].Prefix = XmlPrefix;
RequestDataSet.Tables["item"].Namespace = null;
But that also does not work... Does anyone know a solution for this?

I've come across similar issues when fighting with BizTalk adapters... but that's a different story.
Not sure if there is a different (cleaner) way, but you could always 'grab' the attribute and remove it, as decribed here.
Effectively you would do the following:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);
if (xDoc.DocumentElement.Attributes.Count > 0)
{
XmlAttribute xAtt = xDoc.DocumentElement.Attributes[0];
xDoc.DocumentElement.Attributes.Remove(xAtt);

Related

Retrieving specific data from XML file

Using LINQ to XML.
I have an XML file which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<TileMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>title</Title>
<Abstract>Some clever text about this.</Abstract>
<SRS>OSGEO:41001</SRS>
<Profile>global-mercator or something</Profile>
</TileMap>
I can retrieve the <Title> from this with no problems by using this little piece of code:
string xmlString = AppDomain.CurrentDomain.BaseDirectory + #"Capabilities\" + name + ".xml";
string xmlText = File.ReadAllText(xmlString);
byte[] buffer = Encoding.UTF8.GetBytes(xmlText);
XElement element = XElement.Load(xmlString);
IEnumerable<XElement> title =
from el in element.Elements("Title")
select el;
foreach (XElement el in title)
{
var elementValue = el.Value;
}
However, this isn't very flexible because say I have an XML file that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RootObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Services>
<TileMapService>
<Title>title</Title>
<href>http://localhost/root</href>
</TileMapService>
</Services>
</RootObject>
It can't find <Title> but it finds <Services> (I presume) but since it's not called "Title" it just ignores it. I'm not very strong in working with XML. How would I go about making a method that looks through the XML and fetches me "Title" or however you'd implement this?
You're currently just looking at the child elements of the root element.
Instead, if you want to find all descendants, use Descendants.
Additionally, there's no point in using a query expression of from x in y select x (or rather, there's a very limited point in some cases, but not here). So just use:
var titles = element.Descendants("Title");
Personally I would actually use XDocument here rather than XElement - you have after all got a whole document, complete with XML declaration, not just an element.
Change your LINQ query to:
IEnumerable<XElement> title =
from el in element.Descendants("Title")
select el;
Elements returns only the immediate children, Descendants returns all descendant nodes instead.
Descendants will select all the "Title" elements irrespective of the level. Please use xpath to correctly locate the element
using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
public class Program
{
public static void Main()
{
string xmlFile = AppDomain.CurrentDomain.BaseDirectory + #"Capabilities\" + name + ".xml";
XElement xml=XElement.Load(xmlFile);
IEnumerable<XElement> titleElements = xml.XPathSelectElements("//Services/TileMapService/Title");
}
}

Modify xml, child nodes with same name

I am trying to modify a xmlString so i can create a dataset on the fly.
The xml looks like this:
<?xml version="1.0"?>
<ds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttActivity>
<a-actno>1030371</a-actno>
<a-status>Start</a-status>
<a-solution/>
<a-descript>hei</a-descript>
<a-descript>hopp</a-descript>
<a-acttypegr>0</a-acttypegr>
<a-calltype/>
</ttActivity>
</ds>
The problem when creating a dataset using dataset.ReadXML(xmlReader) is the 2 nodes with same name "a-descript". Is there a quick way to fix this xml so that the nodes get unique names. ie: a-descript1 and a-descript2 ??
using LINQ to XML
XDocument doc = XDocument.Parse(xmlString);
doc.Descendants("a-descript").Last().Name = "a-descript2";
xmlString = doc.ToString();

xml Nodes by Element

Below is an example of the xml file that I need to pull data via C#. This is my first experience with reading xml files and a beginner with xml. Anyone have an example of how I would find/load the fieldorder values for Export_B?
<?xml version="1.0" encoding="utf-8"?>
<Config>
<OutFolderCSV>c:\Output\2012\upload_Files</OutFolderCSV>
<OutFolderImage>c:\Output\2012\NM_Scorecard_Images</OutFolderImage>
<PathOutLogFile>c:\Output\2012\Log\Matches.log</PathOutLogFile>
<FieldSeparator>,</FieldSeparator>
<ExportFile>
<Name>Export_A</Name>
<FieldOrder>matchID</FieldOrder>
<FieldOrder>contactID</FieldOrder>
<FieldOrder>stageID13</FieldOrder>
<FieldOrder>stringScore1a</FieldOrder>
<FieldOrder>xScore1a</FieldOrder>
<FieldOrder>stageID14</FieldOrder>
<FieldOrder>stringScore1b</FieldOrder>
<FieldOrder>xScore1b</FieldOrder>
<FieldOrder>stageID15</FieldOrder>
<FieldOrder>stringScore1c</FieldOrder>
<FieldOrder>xScore1c</FieldOrder>
</ExportFile>
<ExportFile>
<Name>Export_B</Name>
<FieldOrder>matchID</FieldOrder>
<FieldOrder>contactID</FieldOrder>
<FieldOrder>stageID16</FieldOrder>
<FieldOrder>stringScore1a</FieldOrder>
<FieldOrder>xScore1a</FieldOrder>
<FieldOrder>stageID17</FieldOrder>
<FieldOrder>stringScore1b</FieldOrder>
<FieldOrder>xScore1b</FieldOrder>
<FieldOrder>stageID18</FieldOrder>
<FieldOrder>stringScore1c</FieldOrder>
<FieldOrder>xScore</FieldOrder>
</ExportFile>
</Config>
Using LINQ to XML:
var doc = XDocument.Load(#"c:\path\to\file.xml");
var fieldOrders =
from exportFile in doc.Descendants("ExportFile")
where (string)exportFile.Element("Name") == "Export_B"
from fieldOrder in exportFile.Elements("FieldOrder")
select (string)fieldOrder;
I have written an article
http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions
on XML using XDocument object.
You can parse the XML easily using
XDocument.Load(filepath)
Please read the section XLinq to parse the objects.
edit :
You can change value of Export_B using the code :
var document = XDocument.Load(filepath)
var exportFiles = document.Descandants("ExportFile");
List<XElement> list = new List<XElement>();
foreach(var element in exportFiles)
{
list.Add(element);
// Now you can do element.Element("Name") to get the name. Put a breakpoint on this, you can get the reference of all underlying objects.
}

C# - Array from XML as an embedded resource

I've been trying to find a good clean way to load the contents of an XML file into an array to use but I've only found partial answers here and there. My XML file is an Embedded Resource for simplicity, and contains a list of about 115 elements that all contain an id and name attribute.
The XML looks like so:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Item>
<id>1</id>
<name>Example1</name>
</Item>
<Item>
<id>2</id>
<name>Example2</name>
</Item>
<Item>
<id>3</id>
<name>Example3</name>
</Item>
</Items>
I'm able to load everything in and I see my data in the InnerXML but I cannot find out how to access it correctly.
public Form1()
{
InitializeComponent();
assembly = Assembly.GetExecutingAssembly();
XmlDocument xml = null;
try
{
string filePath = "MyProject.ItemList.xml";
Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filePath);
if (fileStream != null)
{
xml = new XmlDocument();
xml.Load(fileStream);
}
}
catch {
//Do nothing
}
XmlDocument itemsFromXML = xml.DocumentElement.InnerXml;
foreach (XmlNode node in itemsFromXML)
{
int id = Convert.ToInt32(node.Attributes.GetNamedItem("id").ToString());
string name = node.Attributes.GetNamedItem("name").ToString();
gameItemList.Add(new GameItem(id, name));
}
}
That's the code I have that would ideally set this array up for me to use, though it is fairly broken due to me trying different things, but I think it conveys the general idea. Hopefully someone can make some sense of it and explain to me what I'm doing horribly wrong (>.<) I would be happy to provide more information, clarification, etc if I missed something important!
Thanks!
Using System.Xml.Linq:
var items = XElement.Load(fileStream)
.Elements("Item")
.Select(itemXml => new {
id = (int)itemXml.Element("id").Value,
name = itemXml.Element("name").Value
})
.ToArray();
Use an xpath.
XmlNodeList nodes = xml.SelectNodes("Items/Item");
foreach ( XmlNode node in nodes )
{
int id = int.Parse(node.SelectSingleNode("id").InnerText);
}

I am trying to read directory from xml file in c# and have problem

<?xml version="1.0" encoding="UTF-8"?>
<form:Documents xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd">
<_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
<_Page_1>
<_First_Name>John</_First_Name>
<_Last_Name>Doe</_Last_Name>
</_Page_1>
</_Document_Definition_1:_Document_Definition_1>
</form:Documents>
I have xml containing directory of pdf file which I would need to read.
I can read first name and last name from _Page_1 node but do not know how to read ImagePath.
Here is my code to read from _Page_1
XDocument xDoc = XDocument.Load("Test.xml");
var poc = from p in xDoc.Descendants("_Page_1")
select new
{
FirstName = p.Element("_First_Name").Value,
LastNumber = p.Element("_Last_Name").Value
};
// Execute the query
foreach (var customer in poc)
{
Console.WriteLine(customer.FirstName);
Console.WriteLine(customer.LastName);
}
//Pause the application
Console.ReadLine();
Thank you BrokenGlass, it's working.
I have one more question.
What if I have several iteration of _Document_Definition node, how do I read each iteration.
<?xml version="1.0" encoding="UTF-8"?>
<form:Documents xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd">
<_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
<_Page_1>
<_First_Name>John</_First_Name>
<_Last_Name>Doe</_Last_Name>
</_Page_1>
</_Document_Definition_1:_Document_Definition_1>
<_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test2.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
<_Page_1>
<_First_Name>Jane</_First_Name>
<_Last_Name>Doe</_Last_Name>
</_Page_1>
</_Document_Definition_1:_Document_Definition_1>
</form:Documents>
You are missing the XML namespace references to access those attributes, this works:
XDocument doc = XDocument.Load(#"test.xml");
XNamespace _Document_Definition_1 = "http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd";
XNamespace addData = "http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd";
string impagePath = doc.Descendants(_Document_Definition_1 + "_Document_Definition_1")
.First()
.Attribute(addData + "ImagePath")
.Value;
It looks like Imagepath is an attribute not an element. Hence you are not able to read it. Check for the attributes in the xml file.

Categories