用于修剪前三行和后四行的C#编程

时间:2016-01-28 18:43:18

标签: c# xml

下面是我的XML文件。我想获得节点" name"来自XML使用C#

'EventObjectsRead' ('73')
message attributes:
SATRCFG_OBJECT [xml] = 
<ConfData>
  <CfgAgentGroup>
    <CfgGroup>
      <DBID value="225"/>
      <tenantDBID value="101"/>
      <name value="CBD"/>
      <routeDNDBIDs>
        <DBID value="825"/>
      </routeDNDBIDs>
      <capacityTableDBID value="0"/>
      <quotaTableDBID value="0"/>
      <state value="1"/>
      <capacityRuleDBID value="0"/>
      <siteDBID value="0"/>
      <contractDBID value="0"/>
    </CfgGroup>
    <agentDBIDs>
      <DBID value="128"/>
      <DBID value="133"/>
      <DBID value="135"/>
      <DBID value="385"/>
      <DBID value="433"/>
    </agentDBIDs>
  </CfgAgentGroup>
</ConfData>

IATRCFG_TOTALCOUNT [int] = 1
IATRCFG_OBJECTCOUNT [int] = 1
IATRCFG_OBJECTTYPE [int] = 5
IATRCFG_REQUESTID [int] = 3

有没有办法获得节点&#34; name&#34;直接从上面的XML或如果我需要修剪前三行和后四行。我怎么能这样做。

3 个答案:

答案 0 :(得分:0)

您可以在原始字符串上使用Regex提取您要查找的节点(其中str是您的字符串数据):

// Use Regex to match the exact string and parse that to XElement.
string nameXML = Regex.Match(str, @"<name +value="".*"" */>").Groups[0].Value;
XElement name = XElement.Parse(nameXML);

或者这里是一个示例,您可以在其中删除无效行,解析XML,然后从XML对象访问数据:

// Split the string into groups using newline as a delimiter.
string[] groups = str.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
// Use skip and take to trim the first 3 and last 4 elements.
// Rejoin the remainder back together with empty strings and parse the XElement.
string xmlString = string.Join(string.Empty, groups.Take(groups.Length - 4).Skip(3));
XElement xml = XElement.Parse(xmlString);
// Use Descendants and First to get the first node called 'name' in the XML.
XElement name = xml.Descendants("name").First();

答案 1 :(得分:-1)

以下是实现此目的的两种方法。使用字符串操作或使用RegEx:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Name: {0}", GetNameFromFileString(File.ReadAllText("file.txt")));
        Console.WriteLine("Name: {0}", GetNameFromFile("file.txt"));
    }

    private static string GetNameFromFileString(string filecontent)
    {
        Regex r = new Regex("(?<Xml><ConfData>.*</ConfData>)", RegexOptions.Singleline);
        var match = r.Match(filecontent);
        var xmlString = match.Groups["Xml"].ToString();
        return GetNameFromXmlString(xmlString);
    }

    private static string GetNameFromFile(string filename)
    {
        var lines = File.ReadAllLines(filename);
        var xml = new StringBuilder();
        var isXml = false;
        foreach (var line in lines)
        {
            if (line.Contains("<ConfData>"))
                isXml = true;
            if (isXml)
                xml.Append(line.Trim());
            if (line.Contains("</ConfData>"))
                isXml = false;
        }

        var text = xml.ToString();
        return GetNameFromXmlString(text);
    }

    private static string GetNameFromXmlString(string text)
    {
        var xDocument = XDocument.Parse(text);
        var cfgAgentGroupt = xDocument.Root.Element("CfgAgentGroup");
        var cfgGroup = cfgAgentGroupt.Element("CfgGroup");
        var name = cfgGroup.Element("name");
        var nameValue = name.Attribute("value");
        var value = nameValue.Value;
        return value;
    }
}

答案 2 :(得分:-2)

从你提供给我们的字符串和你想要做的事情的描述我假设你想从文件中提取XMl。我会用以下方式做到这一点:

        string text = System.IO.File.ReadAllText(@"C:\docs\myfile.txt");
        Regex r = new Regex("<ConfData>(.|\r\n)*?</ConfData>");
        var v = r.Match(text);

        string myResult = "<ConfData>" + v.Groups[0].ToString() + "</ConfData>";