2017-02-15 1 views
1

루트 태그를 제외한 모든 XML 요소에서이 xmlns = "http://www.AB.com/BC/QualityDeviationCase/1_0"을 제거하고 싶습니다. C#을 사용하여 XML 요소. xml 형식으로 코드를 작성하려면 아래 코드를 작성하십시오. 그러나 XML 요소에서 xmlns 태그를 제거 할 수 없습니다.cml을 사용하여 xml 요소에서 xmlns 태그를 제거하는 방법 #

<?xml version="1.0" encoding="utf-16"?> 
<QualityDeviationCaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<CaseID xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">2</CaseID> 
<Description xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">Air filter is not present</Description> 
<StartDate xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">0001-01-01T00:00:00</StartDate> 
<LastUpdated xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">0001-01-01T00:00:00</LastUpdated> 
</QualityDeviationCaseType> 

이후의 xmlns = "http://www.AB.com/BC/QualityDeviationCase/1_0"이 태그를 제거, 내 결과는

<?xml version="1.0" encoding="utf-16"?> 
<QualityDeviationCaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<CaseID>2</CaseID> 
<Description>Air filter is not present</Description> 
<StartDate>0001-01-01T00:00:00</StartDate> 
<LastUpdated>0001-01-01T00:00:00</LastUpdated> 
</QualityDeviationCaseType> 

내 C# 코드 below-으로 다음과 같이 반환해야 , 내 XSD 파일에서 XML 결과를 얻고있다.

connection.Open(); 
adapter = new SqlDataAdapter(sql, connection); 
adapter.Fill(dt); 
myTestTable = dt.Clone(); 
DataRow[] orderRows = dt.Select(); 

XmlDocument xmlDoc = new XmlDocument(); 
QualityDeviationCaseType oQualityDeviationCaseType = new QualityDeviationCaseType(); 

foreach (DataRow row in orderRows) 
{ 
    oQualityDeviationCaseType = new QualityDeviationCaseType(); 
    oQualityDeviationCaseType.CaseID = row[0].ToString(); 
    oQualityDeviationCaseType.Description = row[3].ToString(); 
} 

using (StringWriter stringwriter = new System.IO.StringWriter()) 
{ 
    XmlSerializer ser = new XmlSerializer(typeof(QualityDeviationCaseType)); 
    ser.Serialize(stringwriter, oQualityDeviationCaseType); 
    sampleChannel.Publish(stringwriter.ToString()); 

    //This line of code sending my xml file to IBM WMQ. 
} 

필자의 위 코드에 따르면, 각 결과마다 XML 태그가 포함되어 있습니다. C#을 사용하여 요소 태그에서 제거하고 싶습니다.

답변

0

xmlns 속성은 XML에서 중요한 의미를 가지며 모든 요소를 ​​기본 네임 스페이스 http://www.AB.com/BC/QualityDeviationCase/1_0에 넣습니다.

저는 C#에 익숙하지 않습니다. 그러나 그것을 제거하는 것은 통사론보다는 의미 론적 수준에서 추론을 의미합니다.

구체적으로 내보내는 XML 문서의 모든 요소의 네임 스페이스를 제거하거나 처음 네임 스페이스에 두지 않아야합니다. C# 라이브러리가 완료되면 그렇게 할 수있는 방법이 제공됩니다.

0

정규식을 사용해 보셨습니까?

using System.Text.RegularExpressions; 
... 
    string pattern = @"xmlns=""[a-zA-Z0-9:\/._]{1,}"""; 
    using (StringWriter stringwriter = new System.IO.StringWriter()) 
    { 
     XmlSerializer ser = new XmlSerializer(typeof(QualityDeviationCaseType)); 
     ser.Serialize(stringwriter, oQualityDeviationCaseType); 
     string s = stringwriter.ToString(); 
     Match m = Regex.Match(s,pattern); 
     if(m.Success) 
      s=s.Replace(m.Value, ""); 


     sampleChannel.Publish(s); 
     //This line of code sending my xml file to IBM WMQ. 
    } 

나는 XmlSerializer에 익숙하지 않지만, 나는 그렇게 생각한다. Match는 'xmlns = "'로 시작하고 '"'로 끝나는 항목 만 찾습니다.

관련 문제