Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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
C# 如何在不使用XML属性的情况下读取包含XML元素的字符串_C# - Fatal编程技术网

C# 如何在不使用XML属性的情况下读取包含XML元素的字符串

C# 如何在不使用XML属性的情况下读取包含XML元素的字符串,c#,C#,我正在我的项目中进行XML读取过程。我必须读取XML文件的内容。我已经做到了 出于好奇,我还尝试使用相同的方法,将XML内容保留在字符串中,然后只读取elemet标记中的值。即使这样,我也做到了。下面是我的代码 string xml = <Login-Form> <User-Authentication> <username>Vikneshwar</username>

我正在我的项目中进行XML读取过程。我必须读取XML文件的内容。我已经做到了

出于好奇,我还尝试使用相同的方法,将XML内容保留在字符串中,然后只读取elemet标记中的值。即使这样,我也做到了。下面是我的代码

string xml = <Login-Form>
                 <User-Authentication>
                     <username>Vikneshwar</username>
                     <password>xxx</password>
                 </User-Authentication>

                 <User-Info>
                     <firstname>Vikneshwar</firstname>
                     <lastname>S</lastname>
                     <email>xxx@xxx.com</email>
                 </User-Info>
             </Login-Form>";
        XDocument document = XDocument.Parse(xml);

var block = from file in document.Descendants("client-authentication")
            select new
            {
                Username = file.Element("username").Value,
                Password = file.Element("password").Value,
            };

foreach (var file in block)
{
    Console.WriteLine(file.Username);
    Console.WriteLine(file.Password);
}
stringxml=
维克内斯瓦尔
xxx
维克内斯瓦尔
s
xxx@xxx.com
";
XDocument document=XDocument.Parse(xml);
var block=来自document.subjects中的文件(“客户端身份验证”)
选择新的
{
用户名=file.Element(“用户名”).Value,
密码=file.Element(“密码”).Value,
};
foreach(块中的var文件)
{
Console.WriteLine(file.Username);
Console.WriteLine(file.Password);
}
类似地,我获得了我的另一组元素(firstname、lastname和email)。现在我的好奇心再次吸引了我。现在我正在考虑使用字符串函数做同样的事情


在上面的代码中使用相同的字符串。我尝试不使用任何与XMl相关的类,即、、等。相同的输出应该只使用字符串函数来实现。我不能这样做。可能吗?

您可以使用
字符串
类中提供的
IndexOf、Equals、Substring等方法来实现fulf如需了解更多信息,请根据您的需要进行说明

使用也是一个相当大的选择


但建议使用类来实现此目的。

不使用正则表达式也可以实现,如下所示:

string[] elementNames = new string[]{ "<username>", "<password>"};
foreach (string elementName in elementNames)
{
    int startingIndex = xml.IndexOf(elementName);
    string value = xml.Substring(startingIndex + elementName.Length,
        xml.IndexOf(elementName.Insert(1, "/")) 
        - (startingIndex + elementName.Length));
    Console.WriteLine(value);
}
public static class StringExtension
{
    public static string Between(this string content, string start, string end)
    { 
        int startIndex = content.IndexOf(start) + start.Length;
        int endIndex = content.IndexOf(end);
        string result = content.Substring(startIndex, endIndex - startIndex);
        return result;
    }
}
string[]elementNames=新字符串[]{”“,”“};
foreach(elementNames中的字符串elementName)
{
int startingIndex=xml.IndexOf(elementName);
string value=xml.Substring(startingIndex+elementName.Length,
xml.IndexOf(elementName.Insert(1,“/”)
-(startingIndex+elementName.Length));
控制台写入线(值);
}
使用正则表达式:

string[] elementNames2 = new string[]{ "<username>", "<password>"};
foreach (string elementName in elementNames2)
{
    string value = Regex.Match(xml, String.Concat(elementName, "(.*)",
        elementName.Insert(1, "/"))).Groups[1].Value;
    Console.WriteLine(value);
}
string[]elementNames2=新字符串[]{”“,”“};
foreach(elementNames2中的字符串elementName)
{
string value=Regex.Match(xml,string.Concat(elementName,(.*)),
elementName.Insert(1,“/”))。组[1]。值;
控制台写入线(值);
}

当然,唯一的建议是使用XML解析类

不要这样做。XML比实际情况更复杂,嵌套、字符转义、命名实体、名称空间、排序(属性与元素)规则复杂、注释、未分析的字符数据和空白。例如,只需添加

<!--
    <username>evil</username>
-->


构建一个扩展方法,该方法将获取标签之间的文本,如下所示:

string[] elementNames = new string[]{ "<username>", "<password>"};
foreach (string elementName in elementNames)
{
    int startingIndex = xml.IndexOf(elementName);
    string value = xml.Substring(startingIndex + elementName.Length,
        xml.IndexOf(elementName.Insert(1, "/")) 
        - (startingIndex + elementName.Length));
    Console.WriteLine(value);
}
public static class StringExtension
{
    public static string Between(this string content, string start, string end)
    { 
        int startIndex = content.IndexOf(start) + start.Length;
        int endIndex = content.IndexOf(end);
        string result = content.Substring(startIndex, endIndex - startIndex);
        return result;
    }
}

是的,这是可能的,但不是推荐的或标准的方法。请给我一些提示。我不太熟悉字符串函数。您的XML是否包含特殊(转义)字符、名称空间、cdata?您要求的是重建XElement.Parse()以一种或多或少简化的方式。忘了它吧。我要求的只是知道是否有其他方法可以做到这一点。Ohtwise我已经满足了我的要求。我只是尝试使用字符串函数读取xml。谢谢你的帮助。如果你想写语法分析器,从比xml更简单的东西开始-JSON非常适合这么多的语法比XML更简单的规则。这真是太棒了。谢谢你伊万。伊万我使用了XML类。只是想知道是否还有其他可用的选项。你的帖子给了我很多帮助。非常感谢。事实上,现在已经存在了,我不建议使用旧的、麻烦的
XMLDocument