I am trying to use XML for my GeneralMappingStrategy in Asternet. I have my program working fine using List
such as:
agiServer.MappingStrategy = new GeneralMappingStrategy(
new List<ScriptMapping>()
{
new ScriptMapping() {
ScriptName = "testIVR",
ScriptClass = "Asterisk_Test.testIVR",
}
});
But I'd rather have it read an XML file as it says it can do in the documentation, however it does not seem to say anywhere what the XML format is required.
I have tried:
string pathtoxml = "test.xml";
agiServer.MappingStrategy = new GeneralMappingStrategy(pathtoxml);
With my XML as:
<?xml version="1.0"?>
<ScriptMapping>
<ScriptName>testIVR</ScriptName>
<ScriptClass>Asterisk_Test.testIVR</ScriptClass>
</ScriptMapping>
As a complete guess, seemed to make sense, but this won't compile, I get errors of:
System.InvalidOperationException: 'There was an error reflecting type 'System.Collections.Generic.List`1[AsterNET.FastAGI.MappingStrategies.ScriptMapping]'.'
Does anyone happen to know how to do this?
It appears that there was an issue with the Aster.NET library, I've now submitted the fix and it's been accepted. For anyone who has an issue on this in the future, the XML format is:
<?xml version="1.0"?>
<ArrayOfScriptMapping xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns:xsd="w3.org/2001/XMLSchema">
<ScriptMapping>
<ScriptName>testIVR</ScriptName>
<ScriptClass>Asterisk_newTest.testIVR</ScriptClass>
</ScriptMapping>
</ArrayOfScriptMapping>
Related
Simplified XML file I need to decode:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverylistResponse xmlns:ns2="http://tdriverap3.wsbeans.iseries/">
<return>
<COUNT>3</COUNT>
<DELIVERIES>
<ADD1>1300 address 1</ADD1>
<CITY>NICE CITY</CITY>
<ZIP>85705</ZIP>
</DELIVERIES>
<DELIVERIES>
<ADD1>40 S PINAL PKWY AVE</ADD1>
<CITY>FLORENCE</CITY>
<ZIP>85132</ZIP>
</DELIVERIES>
<DELIVERIES>
<ADD1>1825 EAST MAIN</ADD1>
<CITY>CHANDLER</CITY>
<ZIP>85286</ZIP>
</DELIVERIES>
<ERRORCODE/>
<RUNDATE>09/26/2018</RUNDATE>
</return>
</ns2:deliverylistResponse>
</soap:Body>
</soap:Envelope>
I am using the following to try and decode each of the individual addresses in the code.
I cannot figure out how to access them.
XElement xelement = XElement.Load(#"e:\test\X2.xml");
IEnumerable<XElement> addresses = xelement.Elements();
foreach (var address in addresses)
{
Console.WriteLine(address);
Console.WriteLine(address.Element("CITY").Value);
}
The first writeline works (it outputs the entire XML tree), the second says "System.Xml.Linq.XContainer.Element(...) returned null" - I have tried using DELIVERIES, COUNT, Body etc...
Obviously I am not telling it correctly how to traverse the structure, but I do not know how to go any further with it..
UPDATE: Thanks to some help I have figured out how to do it using Linq.
I would still like to be able to deserialize it if anybody has a pointer.
I followed several tutorials, but the multiple levels of this XML seems to be throwing me off.
I have created a class to hold the data but that is as far as my success with that path has gone.
Thank you,
Joe
Thank you Crowcoder -- this is what I wound up with, which will work.
The real XML file however does have about 60 fields so this is not as good as using a deserialize routine but I can at least move forward with the project.
XElement xelement = XElement.Load(#"e:\test\x2.xml");
IEnumerable<XElement> textSegs =
from seg in xelement.Descendants("DELIVERIES")
select seg;
foreach (var address in textSegs)
{
Console.WriteLine(address.Element("ADD1").Value);
Console.WriteLine(address.Element("CITY").Value);
Console.WriteLine(address.Element("ZIP").Value);
}
Console.ReadKey();
I'm trying to read a WSDL from a URL to dynamically generate the proxy for the WCF service.
This is my code:
XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(description))
if (ServiceDescription.CanRead(xmlTextReader))
{
...
}
I get an XmlException from method ServiceDescription.CanRead.The error massage is "Data at the root level is invalid. Line 1, position 1".
Browsing the WDSL URL in IE, I can see the following tag at the start before tag <wsdl:definitions ...> ... </wsdl:definitions> which doesn't appear in chrome.
<?xml version="1.0" encoding="UTF-8"?>
Could that be the issue? but I suppose ServiceDescription.CanRead should be able to recognise that. Any hints would be appreciated.
Try adding this before the first line included in your question:
var byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (description.StartsWith(byteOrderMarkUtf8))
{
var lastIndexOfUtf8 = byteOrderMarkUtf8.Length - 1;
description = description.Remove(0, lastIndexOfUtf8);
}
Borrowed from here.
This question already has an answer here:
Reading xml file causes HRESULT: 0xC00CE556 [duplicate]
(1 answer)
Closed 6 years ago.
I'm trying to load ";" in a Xml document to my form at the code worked fine until yesterday when I got the error message below for some reson, I haden't made any changes to the code.
Error:
"An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: Data at the root level is invalid. Line 1, position 1."
The Code:
XmlDocument myContacts = new XmlDocument();
string path = "C:\\Users\\Name\\\mycontacts.xml";
private void LoadContacts()
{
myContacts.LoadXml(path);
foreach (XmlNode node in myContacts.SelectNodes("Contacts/Contact"))
{
lstContacts.Items.Add(node.SelectSingleNode("Name").InnerText);
}
}
I've tried Linq (XDocument) to but get the same problem there but at ";" in the Program.cs Main.
Application.Run(new Form1());
I've googled around and tried James Schubert's solution with no result.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<Contact>
<Name>Testing</Name>
<Email>test#gmail.com</Email>
<Phone>070 00 00 000</Phone>
<Street>Test A1</Street>
<Zip>000 00</Zip>
<Town>Testing</Town>
</Contact>
</Contacts>
I know there's lots of threads on the topic already but can't get any of their answers/solutions to work.
Is there any more "magic" ways to deal with this problem than the ones I've been able to find when searching for solutions?
XmlDocument.LoadXml is for parsing an XML string. An example from the docs:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
To load from a file, use XmlDocument.Load.
As an aside, I'd suggest you look at LINQ to XML if you haven't already. It's a much nicer API.
I am trying to deserialize the following XML response using RestSharp:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
<ns0:content>
<ns0:reason>token successfully created</ns0:reason>
<ns0:success>true</ns0:success>
<ns0:authDetails>
<ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
<ns0:expiresIn>604800</ns0:expiresIn>
<ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
</ns0:authDetails>
</ns0:content>
This is a snapshot of my code:
IRestResponse response = client.Execute(request);
RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer();
payload apiresponse = deserial.Deserialize<payload>(response);
And this is the error that I am getting:
An unhandled exception of type 'System.Xml.XmlException' occurred in
System.Xml.dll Additional information: Data at the root level is
invalid. Line 1, position 1.
any ideas what I am doing wrong?
Thanks for all the replies.
I did some more investigation and after printing the content of the response to a string, it turned out that RestSharp was actually converting it from XML to JSON. No idea why it was doing that (i certainly wasn't specifying it, perhaps it's a default setting).
So because the response was a JSON then the XML deserializing was obviously throwing an error!
Thanks again.
Well, the exception message is pretty clear: Line 1 has invalid syntax:
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
The XML should probably look like this instead:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
<ns0:content>
<ns0:reason>token successfully created</ns0:reason>
<ns0:success>true</ns0:success>
<ns0:authDetails>
<ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
<ns0:expiresIn>604800</ns0:expiresIn>
<ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
</ns0:authDetails>
</ns0:content>
</ns0:payload>
If you cannot change how the XML response is generated, you should pre-process the XML using common string-manipulation since it is invalid XML and hence cannot be parsed using standard tools.
How to update in C#3.5 app.config file or Settings.settings file through C# code?
Please provide me the code related to C#3.5 framework support of classes but not with 2.0 framework classes in updating app.config file.
I messed with this issue on a project, and decided depending on circumstance to just use a simple XML config file of my own. The problem is app.config has application level and user level settings for a specific reason. The Code Project article mentioned by others here can get you there, but seems like a lot of work to me.
Easy way, create an XML file:
<?xml version="1.0" encoding="utf-8" ?>
<paths>
<path name="pathtofile1">
<fullpath>\\machine1\folder1\file.txt</fullpath>
</path>
<path name="pathtofile2">
<fullpath>\\machine2\folder2\file2.txt</fullpath>
</path>
</paths>
then use LINQ to get at a node:
XDocument doc = XDocument.Load(pathToXmlfile);
var filePath1 = from c in doc.Descendants("path")
where (string)c.Attribute("name") == "pathtofile1"
select (string)c.Element("fullpath").Value;
string thePath = filePath1.First();
Of course you don't have the built in typing, but this is an easy, generic approach you can use in a lot of situations such as in dll classes.
Now that you are using a 'regular' xml file, you can use the techniques mentioned here to update it. For example and this blog does a nice job.
Possible solution: Changing App.config at Runtime
Please check out this code:
You have to load the web.congif first:
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
Then you can modify or add it just like this:
if (config.AppSettings.Settings["YourTag"] == null)
{
config.AppSettings.Settings.Add("YourTag", "yourValue");
}
else
{
config.AppSettings.Settings["YourTag"].Value = "yourValue";
}
It's an XML file so you can use LINQ to XML to open the file
var appConfigPath = string.Format("{0}{1}.exe.config", Directory.GetCurrent(), Process.GetCurrentProcess().ProcessName);
var appConfig = XDocument.Parse(appConfigPath);
//have at it
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("..\\App.config");
XmlNode node = xmlDoc.SelectSingleNode("configuration/capabilities/single/add");// pass xpath of node
//node.Attributes[1].Value = MethodBase.GetCurrentMethod().Name;
node.Attributes[1].Value = TestContext.CurrentContext.Test.MethodName;
xmlDoc.Save("..\\App.config");
Please write in main methods, it is working for me.