Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XmlDictionaryReader的C#代码_C#_Xml - Fatal编程技术网

使用XmlDictionaryReader的C#代码

使用XmlDictionaryReader的C#代码,c#,xml,C#,Xml,我正在将一个xml文档接收到WCF服务中,并希望验证文档中的特定元素/属性。我可以通过以下方式验证正在读取的内容:- using (XmlDictionaryReader reader = message.GetReaderAtBodyContents()) { string content = reader.ReadOuterXml(); } 但是,我现在想获取XML中特定属性和元素的值。我尝试了reader.GetAttribute(“name”、“namespace”)和read

我正在将一个xml文档接收到WCF服务中,并希望验证文档中的特定元素/属性。我可以通过以下方式验证正在读取的内容:-

using (XmlDictionaryReader reader = message.GetReaderAtBodyContents())
{
    string content = reader.ReadOuterXml();
}
但是,我现在想获取XML中特定属性和元素的值。我尝试了
reader.GetAttribute(“name”、“namespace”)
reader.readtofollow
,后跟
reader.ReadElementContentAsString
,但值总是
null
。我做错了什么


我试过各种不同的组合

string test1 = reader.GetAttribute("retry_number", "ns1 location");
string test2 = reader.GetAttribute("ns1:retry_number", "ns1 location");
XML将不同名称空间中的元素定义为
1
,其中
ns1
已经声明

其他代码是:

reader.ReadToFollowing("ns1:retry_number"); 
string test3 = reader.ReadElementContentAsString()
reader.ReadToFollowing("ns1:retry_number");
reader.MoveToFirstAttribute();
string test4 = reader.Value;
首先,我希望能够成功地阅读元素,然后转到属性。测试变量总是设置为
null
,尽管在第一步中,当我读取外部XML时,我可以在内容变量中看到XML。

使用(var xmlReader=requestMessage.GetReaderAtBodyContents())
{
//查找操作数据的元素
ReadStartElement(operation.Name、operation.Contract.Namespace);
对于(int i=0;i
显示准确的代码片段:您的代码中可能存在错误。Rgds,您的XML是否有任何非默认名称空间?您可以粘贴您处理的XML吗?
using (var xmlReader = requestMessage.GetReaderAtBodyContents())
            {
                // Find the element for the operation's data
                xmlReader.ReadStartElement(operation.Name, operation.Contract.Namespace);

                for (int i = 0; i < parameters.Length; i++)
                {
                    var parameterName = parameters[i].GetCustomAttribute<MessageParameterAttribute>()?.Name ?? parameters[i].Name;
                    xmlReader.MoveToStartElement(parameterName, operation.Contract.Namespace);
                    if (xmlReader.IsStartElement(parameterName, operation.Contract.Namespace))
                    {
                        var serializer = new DataContractSerializer(parameters[i].ParameterType, parameterName, operation.Contract.Namespace);
                        arguments.Add(serializer.ReadObject(xmlReader, verifyObjectName: true));
                    }
                }
            }