.net 将Xml Post数据解析为字典
我有一些SaleForce发到我网站上的帖子.net 将Xml Post数据解析为字典,.net,regex,dictionary,xml-parsing,.net,Regex,Dictionary,Xml Parsing,我有一些SaleForce发到我网站上的帖子 [Route("SendPixel")] [HttpPost] [HttpGet] public string SendPixel(HttpRequestMessage request) { var content = Request.Content.ReadAsStringAsync().Result; //parse } 内容: <?xml version="1.0" encoding="UT
[Route("SendPixel")]
[HttpPost]
[HttpGet]
public string SendPixel(HttpRequestMessage request)
{
var content = Request.Content.ReadAsStringAsync().Result;
//parse
}
内容:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Body>
<Notification>
<sObject xsi:type="sf:Lead" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<sf:Email>wow@vova.wow</sf:Email>
<sf:Phone>1237556969</sf:Phone>
</sObject>
</Notification>
</notifications>
</soapenv:Body>
</soapenv:Envelope>
我怎么做?Regex还是有别的办法
在xml中,您可以看到总是有值
这将完成当前格式的工作
Dictionary<string, string> _dictionary = new Dictionary<string, string>();
string key = "";
string value = "";
string xmlData = Request.Content.ReadAsStringAsync().Result;
using (XmlReader readerTest = XmlReader.Create(new StringReader(xmlData)))
{
while (readerTest.Read())
{
if (readerTest.LocalName.Contains("Notification"))
{
try
{
readerTest.MoveToContent();
while (readerTest.Read())
{
if (readerTest.Name.Contains("sf"))
{
key = readerTest.LocalName;
value = readerTest.ReadElementContentAsString();
if (!string.IsNullOrEmpty(key))
{
_dictionary.Add(key, value);
}
}
}
break;
}
catch (Exception ex) { }
}
}
readerTest.Close();
}
Dictionary\u Dictionary=newdictionary();
字符串键=”;
字符串值=”;
字符串xmlData=Request.Content.ReadAsStringAsync().Result;
使用(XmlReader readerTest=XmlReader.Create(新建StringReader(xmlData)))
{
while(readerTest.Read())
{
if(readerTest.LocalName.Contains(“通知”))
{
尝试
{
readerTest.MoveToContent();
while(readerTest.Read())
{
if(readerTest.Name.Contains(“sf”))
{
key=readerTest.LocalName;
value=readerTest.ReadElementContentAsString();
如果(!string.IsNullOrEmpty(key))
{
_添加(键、值);
}
}
}
打破
}
捕获(例外情况除外){}
}
}
readerTest.Close();
}
检查System.Xml
命名空间。提供的Xml格式不正确…请修复。
<sf:Phone>1237556969</sf:Phone> ==> <sf:KEY>VALUE</sf:KEY>
Dictionary<string, string> _dictionary = new Dictionary<string, string>();
string key = "";
string value = "";
string xmlData = Request.Content.ReadAsStringAsync().Result;
using (XmlReader readerTest = XmlReader.Create(new StringReader(xmlData)))
{
while (readerTest.Read())
{
if (readerTest.LocalName.Contains("Notification"))
{
try
{
readerTest.MoveToContent();
while (readerTest.Read())
{
if (readerTest.Name.Contains("sf"))
{
key = readerTest.LocalName;
value = readerTest.ReadElementContentAsString();
if (!string.IsNullOrEmpty(key))
{
_dictionary.Add(key, value);
}
}
}
break;
}
catch (Exception ex) { }
}
}
readerTest.Close();
}